策略模式定义了算法家族。分别封装起来。让它们之间能够相互替换,此模式让算法的变化独立于使用算法的客户。
Head First设计模式中介绍策略模式时以Duck类作为样例。当中用flyBehavior和quackBehavior两个接口引用变量代表鸭子飞行和鸭子叫这两种行为,通过改变flyBehavior和quackBehavior来满足不同的Duck子类的不同行为。这样带来的优点就是能够在执行时改变Duck子类的行为。以下是我用C++改写的代码。
//MyDuck.h
#ifndef MYDUCK_INCLUDED
#define MYDUCK_INCLUDED //在这里我用抽象类取代原文中的接口
class FlyBehavior{
public:
virtual void fly()=0;
};
class QuackBehavior{
public:
virtual void quack()=0;
};
class FlyWithWings:public FlyBehavior{
public:
void fly();
};
class FlyNoWay:public FlyBehavior{
public:
void fly();
};
class FlyRocketPowerd:public FlyBehavior{
public:
void fly();
};
class Quack:public QuackBehavior{
public:
void quack();
};
class Squeak:public QuackBehavior{
public:
void quack();
};
class MuteQuack:public QuackBehavior{
public:
void quack();
};
class Duck{
public:
Duck();
void swim();
virtual void display()=0;
void performQuack();
void performFly();
void setQuackBehavior(QuackBehavior *newBehavior);
void setFlyBehavior(FlyBehavior *newBehavior);
//在这里我用基类指针取代原文中的接口引用变量
protected:
FlyBehavior *flyBehavior;
QuackBehavior *quackBehavior;
};
class MallardDuck:public Duck{
public:
virtual void display();
MallardDuck();
};
#endif // MYDUCK_INCLUDED
////MyDuck.cpp
#include "MyDuck"
#include <iostream>
using std::cout;
using std::endl;
//定义行为类
void FlyWithWings::fly(){
cout<<"I'm flying with wings!!"<<endl;
}
void FlyNoWay::fly(){
cout<<"I can't fly5555!!"<<endl;
}
void FlyRocketPowerd::fly(){
cout<<"I'm flying with a ROCKET!!"<<endl;
}
void Quack::quack(){
cout<<"Quack!!!"<<endl;
}
void Squeak::quack(){
cout<<"Squeak!!!"<<endl;
}
void MuteQuack::quack(){
cout<<"MuteQuack!!!"<<endl;
}
//定义Duck类的成员方法
void Duck::swim(){
cout<<"I'm swimming!!!"<<endl;
}
void Duck::performQuack(){
quackBehavior->quack();
}
void Duck::performFly(){
flyBehavior->fly();
}
void Duck::setFlyBehavior(FlyBehavior *newBehavior){
flyBehavior=newBehavior;
}
void Duck::setQuackBehavior(QuackBehavior *newBehavior){
quackBehavior=newBehavior;
}
Duck::Duck(){
}
//定义MallardDuck类的成员方法
void MallardDuck::display(){
cout<<"I'm MallardDuck!!!"<<endl;
}
MallardDuck::MallardDuck(){
flyBehavior=new FlyWithWings;
quackBehavior=new Quack;
}
//MyDuckMain.cpp
#include "MyDuck"
int main()
{
Duck *mallard=new MallardDuck;
mallard->display();
mallard->performFly();
mallard->performQuack();
mallard->swim();
mallard->setFlyBehavior(new FlyNoWay);
mallard->performFly();
mallard->setFlyBehavior(new FlyRocketPowerd);
mallard->performFly();
return 0;
}
执行结果为:
I'm MallardDuck!!!
I'm flying with wings!!
Quack!!!
I'm swimming!!!
I can't fly5555!!
I'm flying with a ROCKET!!
相关链接:http://blog.csdn.net/zhangzijiejiayou/article/details/27306819
版权声明:本文博客原创文章,博客,未经同意,不得转载。