Last active
October 17, 2018 04:54
-
-
Save jayden-lee/af90c39d6046adaf696f3f42f0d60fbf to your computer and use it in GitHub Desktop.
Design Patterns : Strategy Pattern
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public abstract class Duck { | |
FlyBehavior flyBehavior; | |
QuackBehavior quackBehavior; | |
public void performQuack() { | |
quackBehavior.quack(); | |
} | |
public void performFly() { | |
flyBehavior.fly(); | |
} | |
public void swim() { | |
System.out.println("swim"); | |
} | |
public abstract void display(); | |
public void setFlyBehavior(FlyBehavior flyBehavior) { | |
this.flyBehavior = flyBehavior; | |
} | |
public void setQuackBehavior(QuackBehavior quackBehavior) { | |
this.quackBehavior = quackBehavior; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Duck { | |
FlyBehavior flyBehavior; | |
QuackBehavior quackBehavior; | |
public void performQuack() { | |
quackBehavior.quack(); | |
} | |
public void swim() { | |
System.out.println("swim"); | |
} | |
public void performFly() { | |
flyBehavior.fly(); | |
} | |
public void display() { | |
System.out.println("display"); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class MallardDuck extends Duck { | |
public MallardDuck() { | |
quackBehavior = new Quack(); | |
flyBehavior = new FlyWithWings(); | |
} | |
@Override | |
public void display() { | |
System.out.println("mallard duck"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment