Strategy パターン
Strategy パターン
Step 4 Strategy パターン
MiniDuckSimulator
Step 1public class MiniDuckSimulator { public static void main(String[] args) { MallardDuck mallard = new MallardDuck(); RedHeadDuck redhead = new RedHeadDuck(); mallard.display(); mallard.performQuack(); mallard.performFly(); System.out.println(); redhead.display(); redhead.performQuack(); redhead.performFly(); } }
Step 2, Step 3
public class MiniDuckSimulator { public static void main(String[] args) { MallardDuck mallard = new MallardDuck(); RedHeadDuck redhead = new RedHeadDuck(); RubberDuck rubber = new RubberDuck(); // 追加 DecoyDuck decoy = new DecoyDuck(); // 追加 mallard.display(); mallard.performQuack(); mallard.performFly(); System.out.println(); redhead.display(); redhead.performQuack(); redhead.performFly(); System.out.println(); // 追加 rubber.display(); rubber.performQuack(); rubber.performFly(); System.out.println(); // 追加 decoy.display(); decoy.performQuack(); decoy.performFly(); } }
Step 4
public class MiniDuckSimulator { public static void main(String[] args) { Duck mallard = new MallardDuck(); Duck redhead = new RedHeadDuck(); Duck rubber = new RubberDuck(); Duck decoy = new DecoyDuck(); mallard.display(); mallard.performQuack(); mallard.performFly(); System.out.println(); redhead.display(); redhead.performQuack(); redhead.performFly(); System.out.println(); rubber.display(); rubber.performQuack(); rubber.performFly(); System.out.println(); decoy.display(); decoy.performQuack(); decoy.performFly(); // 追加 decoy.setFlyBehavior(new FlyRocketPowered()); decoy.performFly(); } }
Duck
Step 1, Step 2public abstract class Duck { public Duck() { } abstract void display(); public void performFly() { System.out.println("I'm flying!!"); } public void performQuack() { System.out.println("Quack"); } }
Step 3
public abstract class Duck { // 追加 FlyBehavior flyBehavior; QuackBehavior quackBehavior; public Duck() { } abstract void display(); public void performFly() { // 変更 flyBehavior.Fly(); } public void performQuack() { // 変更 quackBehavior.Quack(); } }
Step 4
public abstract class Duck { FlyBehavior flyBehavior; QuackBehavior quackBehavior; public Duck() { } // 追加 public void setFlyBehavior (FlyBehavior fb) { flyBehavior = fb; } // 追加 public void setQuackBehavior(QuackBehavior qb) { quackBehavior = qb; } abstract void display(); public void performFly() { flyBehavior.Fly(); } public void performQuack() { quackBehavior.Quack(); } }
FlyBehavior
public interface FlyBehavior { public void Fly(); }
FlyWithWings
public class FlyWithWings implements FlyBehavior { public void Fly() { System.out.println("I'm flying!!"); } }
FlyNoWay
public class FlyNoWay implements FlyBehavior { public void Fly() { System.out.println("I can't fly"); } }
FlyRocketPowered
追加public class FlyRocketPowered implements FlyBehavior { public void Fly() { System.out.println("I'm flying with a rocket"); } }
QuackBehavior
public interface QuackBehavior { public void Quack(); }
Quack
public class Quack implements QuackBehavior { public void Quack() { System.out.println("Quack"); } }
Squeak
public class Squeak implements QuackBehavior { public void Quack() { System.out.println("Squeak"); } }
MuteQuack
public class MuteQuack implements QuackBehavior { public void Quack() { System.out.println("<< Silence >>"); } }
MallardDuck
Step 1, Step 2public class MallardDuck extends Duck { public MallardDuck() { } public void display() { System.out.println("I'm a real Mallard duck"); } }
Step 3
public class MallardDuck extends Duck { public MallardDuck() { // 追加 flyBehavior = new FlyWithWings(); quackBehavior = new Quack(); } public void display() { System.out.println("I'm a real Mallard duck"); } }
RedHeadDuck
Step 1, Step 2public class RedHeadDuck extends Duck { public RedHeadDuck() { } public void display() { System.out.println("I'm a real Red Headed duck"); } }
Step 3
public class RedHeadDuck extends Duck { public RedHeadDuck() { // 追加 flyBehavior = new FlyWithWings(); quackBehavior = new Quack(); } public void display() { System.out.println("I'm a real Red Headed duck"); } }
RubberDuck
Step 2public class RubberDuck extends Duck { public RubberDuck() { } public void display() { System.out.println("I'm a rubber duckie"); } // オーバーライド public void performFly() { System.out.println("I can't fly"); } // オーバーライド public void performQuack() { System.out.println("Squeak"); } }
Step 3
public class RubberDuck extends Duck { public RubberDuck() { // 追加 flyBehavior = new FlyNoWay(); quackBehavior = new Squeak(); } public void display() { System.out.println("I'm a rubber duckie"); } }
DecoyDuck
Step 2public class DecoyDuck extends Duck { public DecoyDuck() { } public void display() { System.out.println("I'm a duck Decoy"); } // オーバーライド public void performFly() { System.out.println("I can't fly"); } // オーバーライド public void performQuack() { System.out.println("<< Silence >>"); } }
Step 3
public class DecoyDuck extends Duck { public DecoyDuck() { // 追加 flyBehavior = new FlyNoWay(); quackBehavior = new MuteQuack(); } public void display() { System.out.println("I'm a duck Decoy"); } }
実行結果
I'm a real Mallard duck Quack I'm flying!! I'm a real Red Headed duck Quack I'm flying!! I'm a rubber duckie Squeak I can't fly I'm a duck Decoy << Silence >> I can't fly I'm flying with a rocket