HeadFirst设计模式之策略模式
什么是策略模式:它定义了一系列算法,可以根据不同的实现调用不同的算法
大多数的设计模式都是为了解决系统中变化部分的问题
一、OO基础
抽象、封装、多态、继承
二、OO原则
1.封装变化,如把FlyBehavious、QuackBehavior从Duck中抽离出来
2.组合优于继承
3.面向抽象(接口、抽象类)编程而不是面向实现
4.为对象的解耦设计而努力
5.类应该对扩展开放,对修改封闭
6.依赖抽象,不要依赖具体
7.Principle of Least Knowledge -talk only to your immediate friends.
8.A class should have only one reason to change.
三、设计过程
1.不合理地用继承
2.需求变化,要增加飞行
3.继承的弊端出现,不需要的也会被继承
4.用接口也不合适
5.封装变化部分
6.策略模式,使不同的子类可以重用行为,且可以随时添加新行为而不影响现有代码
7.
四、代码实现
1.
1 package headfirst.designpatterns.strategy; 2 3 public abstract class Duck { 4 FlyBehavior flyBehavior; 5 QuackBehavior quackBehavior; 6 7 public Duck() { 8 } 9 10 public void setFlyBehavior(FlyBehavior fb) { 11 flyBehavior = fb; 12 } 13 14 public void setQuackBehavior(QuackBehavior qb) { 15 quackBehavior = qb; 16 } 17 18 abstract void display(); 19 20 public void performFly() { 21 flyBehavior.fly(); 22 } 23 24 public void performQuack() { 25 quackBehavior.quack(); 26 } 27 28 public void swim() { 29 System.out.println("All ducks float, even decoys!"); 30 } 31 }
2.
package headfirst.designpatterns.strategy; public interface FlyBehavior { public void fly(); }
3.
package headfirst.designpatterns.strategy; public interface QuackBehavior { public void quack(); }
4.
1 package headfirst.designpatterns.strategy; 2 3 public class MallardDuck extends Duck { 4 5 public MallardDuck() { 6 7 quackBehavior = new Quack(); 8 flyBehavior = new FlyWithWings(); 9 10 } 11 12 public void display() { 13 System.out.println("I'm a real Mallard duck"); 14 } 15 }
5.
package headfirst.designpatterns.strategy; 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"); } }
6.
package headfirst.designpatterns.strategy; public class FlyWithWings implements FlyBehavior { public void fly() { System.out.println("I'm flying!!"); } }
7.
1 package headfirst.designpatterns.strategy; 2 3 public class FlyRocketPowered implements FlyBehavior { 4 public void fly() { 5 System.out.println("I'm flying with a rocket"); 6 } 7 }
8.
package headfirst.designpatterns.strategy; public class Quack implements QuackBehavior { public void quack() { System.out.println("Quack"); } }
9.
1 package headfirst.designpatterns.strategy; 2 3 public class Squeak implements QuackBehavior { 4 public void quack() { 5 System.out.println("Squeak"); 6 } 7 }
10.
1 package headfirst.designpatterns.strategy; 2 3 public class MiniDuckSimulator { 4 5 public static void main(String[] args) { 6 7 MallardDuck mallard = new MallardDuck(); 8 RubberDuck rubberDuckie = new RubberDuck(); 9 DecoyDuck decoy = new DecoyDuck(); 10 11 Duck model = new ModelDuck(); 12 13 mallard.performQuack(); 14 rubberDuckie.performQuack(); 15 decoy.performQuack(); 16 17 model.performFly(); 18 model.setFlyBehavior(new FlyRocketPowered()); 19 model.performFly(); 20 } 21 }
11.
1 package headfirst.designpatterns.strategy; 2 3 public class MiniDuckSimulator1 { 4 5 public static void main(String[] args) { 6 7 Duck mallard = new MallardDuck(); 8 mallard.performQuack(); 9 mallard.performFly(); 10 11 Duck model = new ModelDuck(); 12 model.performFly(); 13 model.setFlyBehavior(new FlyRocketPowered()); 14 model.performFly(); 15 16 } 17 }
五.练习:在游戏中如何给不同角色赋予不同的武器
You can do anything you set your mind to, man!