Strategy パターン
Strategy パターン
Step 1
MiniDuckSimulator
public 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(); } }
Duck
public abstract class Duck { public Duck() { } abstract void display(); public void performFly() { System.out.println("I'm flying!!"); } public void performQuack() { System.out.println("Quack"); } }
MallardDuck
public class MallardDuck extends Duck { public MallardDuck() { } public void display() { System.out.println("I'm a real Mallard duck"); } }
RedHeadDuck
public class RedHeadDuck extends Duck { public RedHeadDuck() { } public void display() { System.out.println("I'm a real Red Headed duck"); } }
実行結果
I'm a real Mallard duck Quack I'm flying!! I'm a real Red Headed duck Quack I'm flying!!