设计模式之策略模式
策略模式:定义了算法族,分别封装起来,让他们之间可以互相替换,此模式让算法的变化独立于适用算法的客户。
注:首先我用的是Head First 设计模式这本书,里面的所有代码都放在了博主的GitHub里面,地址:https://github.com/Stray-Kite/Design-Pattern ,读者们可以自行下载哦。注:本次的代码在策略模式那个文档里,也就是strategy文档里面。
下面是这一节的最终代码:
首先让我们看一下这个项目的整体架构,图不是很清楚,但是应该还是能看的八九不离十,望读者见谅;有些功能没有写,但是在GitHub
里面有,读者可以自行下载研究哦,其实都是一些重复操作,哈哈:
Duck.java:
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 }
输入并编译FlyBehavior接口(FlyBehavior.java)与三个行为实现类(FlyWithWings.java和FylNoWay.java和FlyRocketPowered.java)
FlyBehavior.java(管鸭子的飞行行为):
1 package headfirst.designpatterns.strategy; 2 3 public interface FlyBehavior { 4 public void fly(); 5 }
FlyWithWings.java:
1 package headfirst.designpatterns.strategy; 2 3 public class FlyWithWings implements FlyBehavior { 4 public void fly() { 5 System.out.println("I'm flying!!"); 6 } 7 }
FlyNoWay.java:
1 package headfirst.designpatterns.strategy; 2 3 public class FlyNoWay implements FlyBehavior { 4 public void fly() { 5 System.out.println("I can't fly"); 6 } 7 }
FlyRocketPowered.java(火箭动力飞行):
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 }
输入并编译QuackBehavior接口(QuackBehavior.java)与三个行为实现类(Quack.java和MuteQuack.java和Squeak.java)
QuackBehavior.java(管鸭子叫声行为):
1 package headfirst.designpatterns.strategy; 2 3 public interface QuackBehavior { 4 public void quack(); 5 }
Quack.java:
1 package headfirst.designpatterns.strategy; 2 3 public class Quack implements QuackBehavior { 4 public void quack() { 5 System.out.println("Quack"); 6 } 7 }
MuteQuack.java:
1 package headfirst.designpatterns.strategy; 2 3 public class MuteQuack implements QuackBehavior { 4 public void quack() { 5 System.out.println("<< Silence >>"); 6 } 7 }
Squeak.java:
1 package headfirst.designpatterns.strategy; 2 3 public class Squeak implements QuackBehavior { 4 public void quack() { 5 System.out.println("Squeak"); 6 } 7 }
创建一个绿头鸭:
package headfirst.designpatterns.strategy; public class MallardDuck extends Duck { public MallardDuck() { quackBehavior = new Quack(); flyBehavior = new FlyWithWings(); } public void display() { System.out.println("I'm a real Mallard duck"); } }
在创建一个模型鸭:
1 package headfirst.designpatterns.strategy; 2 3 public class ModelDuck extends Duck { 4 public ModelDuck() { 5 flyBehavior = new FlyNoWay(); 6 quackBehavior = new Quack(); 7 } 8 9 public void display() { 10 System.out.println("I'm a model duck"); 11 } 12 }
最后让我们来一个测试类(MiniDuckSimulator.java):
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 }
运行结果如下图所示: