ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 策略模式(strategy)

策略模式(strategy)

原创 Linux操作系统 作者:xw_z 时间:2009-08-31 17:40:01 0 删除 编辑

策略模式(strategy

 

Ø         定义:

策略模式定义了算法簇,分别封装起来,让它们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。

 

Ø         例一:鸭子(Duck

原理:鸭子叫和飞的行为经常变化,把鸭子叫(QuackBehavior)和飞(FlyBehavior)的行为从鸭子(Duck)类的定义中抽取出来,分别封装为一个抽象基类(设计原则:找出程序中会变化的方面,然后将其和固定不变的方面相分离,即封装变化),各种具体的叫和飞的行为分别继承自这两个基类,而在鸭子(Duck)类中持有指向QuackBehaviorFlyBehavior的指针(设计原则:多用组合,少用继承)。这样鸭子叫和飞的行为可以很容易的扩充,通过在鸭子(Duck)类中定义一个设置行为的接口(SetFlyBehavior,SetQuackBehavior,则可以动态改变鸭子的行为。

C++代码:

//Duck.h

#ifndef DUCK_H

#define DUCK_H

#include

#include

#include "FlyBehavior.h"

#include "QuackBehavior.h"

using std::cout;

using std::endl;

using std::tr1::shared_ptr;

class Duck

{

protected:

     shared_ptr flyBehavior;

     shared_ptr quackBehavior;

public:

     virtual ~Duck(){}

     void setFlyBehavior(const shared_ptr& fly){flyBehavior. = fly;}

     void setQuackBehavior(const shared_ptr& quack){quackBehavior. = quack;}

     void swim() const{cout << "all ducks float,even decoys!" << endl;}

     void performFly(){flyBehavior->fly();};

     void performQuack(){quackBehavior->quack();}

     virtual void display() = 0;

};

class MallardDuck : public Duck

{

public:

     MallardDuck(){flyBehavior. = shared_ptr(new FlyWithWings);quackBehavior. = shared_ptr(new Quack);}

     void display(){cout << "I'm a real Mallard duck" << endl;}

};

class ModelDuck : public Duck

{

public:

     ModelDuck(){flyBehavior. = shared_ptr(new FlyNoWay);quackBehavior. = shared_ptr(new Quack);}

     void display(){cout << "I'm a model duck" << endl;}

};

#endif

 

//FlyBehavior.h

#ifndef FLYBEHAVIOR_H

#define FLYBEHAVIOR_H

#include

using std::cout;

using std::endl;

class FlyBehavior

{

public:

     virtual void fly() const = 0;

};

class FlyWithWings : public FlyBehavior

{

public:

     void fly() const{cout << "I'm flying!!" << endl;}

};

class FlyNoWay : public FlyBehavior

{

public:

     void fly() const{cout << "I can't fly!!" << endl;}

};

class FlyRocketPowered : public FlyBehavior

{

     void fly() const{cout << "I'm flying with a rocket!" << endl;}

};

#endif

 

//QuackBehavior.h

#ifndef QUACKBEHAVIOR_H

#define QUACKBEHAVIOR_H

#include

using std::cout;

using std::endl;

class QuackBehavior

{

public:

     virtual void quack() const = 0;

};

class Quack : public QuackBehavior

{

public:

     void quack() const{cout << "Quack" << endl;}

};

class MuteQuack : public QuackBehavior

{

public:

     void quack() const{cout << "<>" << endl;}

};

class Squeak : public QuackBehavior

{

public:

     void quack() const{cout << "Squeac" << endl;}

};

#endif

//测试文件

#include "DUCK.h"

int main()

{

     Duck* model = new ModelDuck;

     model->performFly();

     model->setFlyBehavior(shared_ptr(new FlyRocketPowered));

     model->performFly();

     delete model;

     return 0;

}

结果为:

I can't fly!!

I'm flying with a rocket!

 

Ø         例二:动作冒险游戏

规则:每个游戏角色一次只能使用一种武器,但是可以在游戏的过程中换武器。

原理:将武器从游戏角色的定义中抽取出来封装为一个单独的抽象基类,各种具体武器继承自该基类,每个角色持有一个指向该基类的指针,并有一个动态改变武器的接口。

C++代码示例:

//Character.h

#ifndef Character_H

#define Character_H

#include

#include "WeaponBehavior.h"

using std::tr1::shared_ptr;

class Character

{

protected:

     shared_ptr weapon;

public:

     void fight(){weapon->useWeapon();}

     virtual void display() = 0;

     virtual ~Character(){};

     void setWeapon(const shared_ptr& weaponBehavior){weapon = weaponBehavior;}

};

class King : public Character

{

public:

     King() {weapon = shared_ptr(new KnifeBehavior);}

     void display(){cout << "I'm the king!!" << endl;}

};

class Queen : public Character

{

public:

     Queen() {weapon = shared_ptr(new AxeBehavior);}

     void display(){cout << "I'm the queen!!" << endl;}

};

class Troll : public Character

{

public:

     Troll() {weapon = shared_ptr(new BowAndArrowBehavior);}

     void display(){cout << "I'm the troll!!" << endl;}

};

class Knight : public Character

{

public:

     Knight() {weapon = shared_ptr(new SwordBehavior);}

     void display(){cout << "I'm the knight!!" << endl;}

};

#endif

 

//WeaponBehavior.h

#ifndef WeaponBehavior_H

#define WeaponBehavior_H

#include

using std::cout;

using std::endl;

class WeaponBehavior

{

public:

     virtual void useWeapon() = 0;

};

class KnifeBehavior. public WeaponBehavior

{

public:

     void useWeapon() {cout << "I have a kinfe!!" << endl;}

};

class AxeBehavior. public WeaponBehavior

{

public:

     void useWeapon() {cout << "I have an Axe!!" << endl;}

};

class BowAndArrowBehavior. public WeaponBehavior

{

public:

     void useWeapon() {cout << "I have a BowAndArrow!!" << endl;}

};

class SwordBehavior. public WeaponBehavior

{

public:

     void useWeapon() {cout << "I have a Sword!!" << endl;}

};

#endif

 

//测试文件

#include "character.h"

int main()

{

     Character* person = new King;

     person->display();

     person->fight();

     person->setWeapon(shared_ptr(new SwordBehavior));

     person->fight();

     return 0;

}

结果为:

I'm the king!!

I have a kinfe!!

I have a Sword!!

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/17014649/viewspace-613644/,如需转载,请注明出处,否则将追究法律责任。

上一篇: 数据库表(二)
下一篇: 没有了~
请登录后发表评论 登录
全部评论

注册时间:2009-05-22

  • 博文量
    28
  • 访问量
    38707