Strategy Design Pattern - The Koch Family
The Koch Family The Koch Family

Latest news

جاري التحميل ...

Strategy Design Pattern

For example, I have an program with an Animal abstract class and two sub-classes Dog and Bird. I want to add a new behavior for the class Animal, this is "fly".  Now, I face to two approaches to solve this issue:

1. Adding an abstract method "fly" into the class Animal. Then, I force the sub-classes should be implemented this method, something like:

public abstract class Animal{
//bla bla
public abstract void fly();
}

public class Bird extends Animal{
//bla bla
public void fly(){
System.out.println("Fly high");
}
}

public class Dog extends Animal{
//bla bla
public void fly(){
System.out.println("Cant fly");
}
}

2. Creating an interfaces with method "fly" inside. The same issue to abstract class, I force the classes these implement this interface should have a method "fly" inside:

public interface Flyable{
public void fly();
}
public class Bird implements Flyable{
//bla bla
public void fly(){
System.out.println("Fly high");
}
}

public class Dog implements Flyable{
//bla bla
public void fly(){
System.out.println("Cant fly");
}
}

These approaches are the OOP basics in order to solve the problem when we want to add a new  behavior, but there is a disadvantage because all sub-classes are forced to implement this behavior even it doesn't make sense for some classes. And, there is no reused code by using interfaces only.

That is where Strategy Design Pattern can help. The following is an example code:

public interface Flyable{
public void fly();
}

public class ItFlys implements Flyable{
public void fly(){
System.out.println("Fly high");
}
}

public class CantFly implements Flyable{
public void fly(){
System.out.println("Cant fly");
}
}

public abstract class Animal{
//bla bla
Flyable flyType;

public void tryToFly(){
flyType.fly();
}
public void setFlyAbility(Flyable newFlyType){
flyType = newFlyType;
}
}

public class Bird extends Animal{
//bla bla
public Bird(){
flyType = new ItFlys ();
}
}

public class Dog extends Animal{
//bla bla
public Dog (){
flyType = new CantFly ();
}
}

This is a diagram that shows the example above:
src: https://www.youtube.com/watch?v=-NCgRD9-C6o&index=3&list=PLF206E906175C7E07

References:

Comments



If you like the content of our blog, we hope to stay in constant communication, just enter your email to subscribe to the blog's express mail to receive new blog updates, and you can send a message by clicking on the button next ...

إتصل بنا

About the site

author The Koch Family <<  Welcome! I'm so glad that you stopped by Your Modern Family blog. Together, we will talk about raising kids, organizing the home and saving money! and Tips & tricks and more…

< Learn more ←

Blog stats

Sparkline 2513183

All Copyrights Reserved

The Koch Family

2020