策略模式(Strategy Pattern) 详解
本文地址: http://blog.csdn.net/caroline_wendy/article/details/26577879
本文版权所有, 禁止转载, 如有需要, 请站内联系.
策略模式: 定义了算法族, 分别封装起来, 让它们之间可以相互替换, 此模式让算法的变化独立于使用算法的客户.
对于父类的子类族需要经常扩展新的功能, 为了使用父类比较灵活的添加子类, 把父类的行为写成接口(interface)的形式;
使用set()方法, 重新设定接口的行为. 提供行为的调用的方法(如perform), 需要调用接口(interface)的固有方法(fly & quack)即可.
代码:
package strategy; public abstract class Duck { //抽象类 FlyBehavior flyBehavior; QuackBehavior quackBehavior; public Duck() { //构造函数 } public abstract void display(); //抽象函数 public void performFly() { flyBehavior.fly(); } public void performQuack() { quackBehavior.quack(); } public void swim() { System.out.println("All ducks float, even decoys! "); } public void setFlyBehavior(FlyBehavior fb) { flyBehavior = fb; } public void setQuackBehavior(QuackBehavior qb) { quackBehavior = qb; } }
FlyBehavior 和 QuackBehavior是两个接口(interface), 包含方法fly()和quack();
继承接口的方法, 都必须使用fly()和quack()的方法;
代码:
/** * @time 2014.5.22 */ package strategy; /** * @author C.L.Wang * */ public interface FlyBehavior { public void fly(); }
/** * @time 2014.5.22 */ package strategy; /** * @author C.L.Wang * */ public interface QuackBehavior { public void quack(); }
接口的实现.
代码:
/** * @time 2014.5.22 */ package strategy; /** * @author C.L.Wang * */ public class FlyNoWay implements FlyBehavior { /* (non-Javadoc) * @see strategy.FlyBehavior#fly() */ @Override public void fly() { // TODO Auto-generated method stub System.out.println("I can't fly! "); } }
/** * */ package strategy; /** * @author Administrator * */ public class FlyRocketPowerd implements FlyBehavior { /* (non-Javadoc) * @see strategy.FlyBehavior#fly() */ @Override public void fly() { // TODO Auto-generated method stub System.out.println("I'm flying with a rocket! "); } }
/** * */ package strategy; /** * @author Administrator * */ public class FlyRocketPowerd implements FlyBehavior { /* (non-Javadoc) * @see strategy.FlyBehavior#fly() */ @Override public void fly() { // TODO Auto-generated method stub System.out.println("I'm flying with a rocket! "); } }
/** * */ package strategy; /** * @author Administrator * */ public class MuteQuack implements QuackBehavior { /* (non-Javadoc) * @see strategy.QuackBehavior#quack() */ @Override public void quack() { // TODO Auto-generated method stub System.out.println("<< Silence >>"); } }
/** * */ package strategy; /** * @author Administrator * */ public class Quack implements QuackBehavior { /* (non-Javadoc) * @see strategy.QuackBehavior#quack() */ @Override public void quack() { // TODO Auto-generated method stub System.out.println("Quack! "); } }
/** * */ package strategy; /** * @author Administrator * */ public class Squeak implements QuackBehavior { /* (non-Javadoc) * @see strategy.QuackBehavior#quack() */ @Override public void quack() { // TODO Auto-generated method stub System.out.println("Squeak"); } }
继承父类的子类, 只需要指定所使用的行为(即 接口(interface)的实现)的类型, 并提供自己的display()函数即可;
代码:
/** * */ package strategy; /** * @author Administrator * */ public class MallardDuck extends Duck { public MallardDuck() { quackBehavior = new Quack(); flyBehavior = new FlyWithWings(); } /* (non-Javadoc) * @see strategy.Duck#display() */ @Override public void display() { // TODO Auto-generated method stub System.out.println("I'm a real Mallard duck! "); } }
/** * */ package strategy; /** * @author Administrator * */ public class ModelDuck extends Duck { public ModelDuck() { flyBehavior = new FlyNoWay(); quackBehavior = new Quack(); } /* (non-Javadoc) * @see strategy.Duck#display() */ @Override public void display() { // TODO Auto-generated method stub System.out.println("I'm a model duck. "); } }
执行时, 只需要执行各个子类继承父类的方法(perform), 即可, 也可以使用set()方法改变其行为;
代码:
/** * */ package strategy; /** * @author Administrator * */ public class MiniDuckSimulator { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Duck mallard = new MallardDuck(); mallard.performQuack(); mallard.performFly(); Duck model = new ModelDuck(); model.performFly(); model.performQuack(); model.setFlyBehavior(new FlyRocketPowerd()); model.performFly(); } }
面向对象的原则:
1. 封装变化的部分;
2. 多用组合, 少用继承;
3. 针对接口编程, 不针对实现编程;
设计模式 - 策略模式(Strategy Pattern) 详解,布布扣,bubuko.com
时间: 2024-12-26 19:16:30