策略模式(Strategy Pattern):定义算法族,分别封装起来,让它们之间可以互相替换,比模式让算法的变化独立于使用算法的客户.
实例:需要实现鸭子的行为(叫和飞),但有些鸭子是不会飞的,而且不同鸭子的叫声不同.
分析:如果使用继承的方法,代码会在多个子类重复,运行时的行为不容易改变,改变会牵一发动全身
代码:
JAVA
- interface FlyBehavior
- {
- public void fly();
- }
- class FlyWithWings implements FlyBehavior
- {
- public void fly()
- {
- System.out.println("I'm flying");
- }
- }
- class FlyNoWay implements FlyBehavior
- {
- public void fly()
- {
- System.out.println("I can't fly");
- }
- }
- class FlyRocketPowered implements FlyBehavior
- {
- public void fly()
- {
- System.out.println("I'm flying with a rocket");
- }
- }
- interface QuackBehavior
- {
- public void quack();
- }
- class Quack implements QuackBehavior
- {
- public void quack()
- {
- System.out.println("Quack");
- }
- }
- class MuteQuack implements QuackBehavior
- {
- public void quack()
- {
- System.out.println("<< Slience >>");
- }
- }
- class Squeak implements QuackBehavior
- {
- public void quack()
- {
- System.out.println("Squeak");
- }
- }
- abstract class Duck
- {
- FlyBehavior flyBehavior;
- QuackBehavior quackBehavior;
- public abstract void display();
- public void performFly()
- {
- flyBehavior.fly();
- }
- public void performQuack()
- {
- quackBehavior.quack();
- }
- public void swim()
- {
- System.out.print("All ducks float");
- }
- public void setFlyBehavior(FlyBehavior fb)
- {
- flyBehavior = fb;
- }
- public void setQuackBehavior(QuackBehavior qb)
- {
- quackBehavior = qb;
- }
- }
- class MallardDuck extends Duck
- {
- public MallardDuck()
- {
- flyBehavior = new FlyWithWings();
- quackBehavior = new Quack();
- }
- public void display()
- {
- System.out.println("I'm a real Mallard duck");
- }
- }
- class ModelDuck extends Duck
- {
- public ModelDuck()
- {
- flyBehavior = new FlyNoWay();
- quackBehavior = new Quack();
- }
- public void display()
- {
- System.out.println("I'm a Model duck");
- }
- }
- public class MiniDuckSimulator
- {
- public static void main(String[] args)
- {
- Duck mallard = new MallardDuck();
- mallard.performFly();
- mallard.performQuack();
- Duck model = new ModelDuck();
- model.performFly();
- model.setFlyBehavior(new FlyRocketPowered());
- model.performFly();
- }
- }
C++
- // MiniDuckSimulator.cpp : 定义控制台应用程序的入口点。
- //
- #include "stdafx.h"
- #include <iostream>
- using namespace std;
- class FlyBehavior//用抽象类替接口
- {
- public:
- virtual void fly()=0;
- };
- class FlyWithWings :public FlyBehavior
- {
- public:
- void fly()
- {
- cout<<"I'm flying/n";
- }
- };
- class FlyNoWay :public FlyBehavior
- {
- public:
- void fly()
- {
- cout<<"I can't fly/n";
- }
- };
- class FlyRocketPowered :public FlyBehavior
- {
- public:
- void fly()
- {
- cout<<"I'm flying with a rocket/n";
- }
- };
- class QuackBehavior
- {
- public:
- virtual void quack()=0;
- };
- class Quack:public QuackBehavior
- {
- public:
- void quack()
- {
- cout<<"Quack/n";
- }
- };
- class MuteQuack:public QuackBehavior
- {
- public:
- void quack()
- {
- cout<<"<< Slience >>/n";
- }
- };
- class Squeak:public QuackBehavior
- {
- public:
- void quack()
- {
- cout<<"Squeak/n";
- }
- };
- class Duck
- {
- protected:
- FlyBehavior *flyBehavior;
- QuackBehavior *quackBehavior;
- public:
- virtual void display()
- {
- }
- void performFly()
- {
- flyBehavior->fly();
- }
- void performQuack()
- {
- quackBehavior->quack();
- }
- void swim()
- {
- cout<<"All ducks float/n";
- }
- void setFlyBehavior(FlyBehavior* fb)
- {
- if(flyBehavior!=NULL)
- delete flyBehavior;
- flyBehavior = fb;
- }
- void setQuackBehavior(QuackBehavior* qb)
- {
- if(quackBehavior!=NULL)
- delete quackBehavior;
- quackBehavior = qb;
- }
- ~Duck()
- {
- if(flyBehavior!=NULL)
- delete flyBehavior;
- if(quackBehavior!=NULL)
- delete quackBehavior;
- }
- };
- class MallardDuck: public Duck
- {
- public:
- MallardDuck()
- {
- flyBehavior = new FlyWithWings();
- quackBehavior = new Quack();
- }
- void display()
- {
- cout<<"I'm a real Mallard duck/n";
- }
- };
- class ModelDuck: public Duck
- {
- public:
- ModelDuck()
- {
- flyBehavior = new FlyNoWay();
- quackBehavior = new Quack();
- }
- void display()
- {
- cout<<"I'm a Model duck/n";
- }
- };
- int _tmain(int argc, _TCHAR* argv[])
- {
- Duck *mallard = new MallardDuck();
- mallard->performFly();
- mallard->performQuack();
- delete mallard;
- Duck* model=new ModelDuck();
- model->performFly();
- model->setFlyBehavior(new FlyRocketPowered());
- model->performFly();
- return 0;
- }
结果: