标签:
策略模式:
定义了算法族, 分别分装起来,让他们之间可以互相替换,此模式让算法的变化独立于使用算法的客户。
找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起。 (即封装变化点)
把会变化的部分取出并封装起来,以便以后可以轻易地改动或扩充此部分,而不影响不需要变化的其他部分。
如果每次新的需求一来,都会使某方面的代码发生变化,那么你就可以确定,这部分代码需要被抽出来,和其他稳定的代码有所区分。
#include <iostream> /* 非策略模式, 以后子类越多, 每当发生变化时都要稍微修改一下所有子类 class Duck { public: Duck(){} virtual ~Duck(){} public: virtual void Quack(){std::cout << "Duck:Quack()" << std::endl;} virtual void Swim(){std::cout << "Duck:Swim()" << std::endl;} virtual void Display(){std::cout << "Duck:Display()" << std::endl;} virtual void Fly(){std::cout << "Duck:Fly()" << std::endl;} }; class MallardDuck: public Duck { public: MallardDuck(){} virtual ~MallardDuck(){} public: void Quack(){std::cout << "MallardDuck:Quack()" << std::endl;} void Swim(){std::cout << "MallardDuck:Swim()" << std::endl;} void Display(){std::cout << "MallardDuck:Display()" << std::endl;} void Fly(){std::cout << "MallardDuck:Fly()" << std::endl;} }; class RedheadDuck: public Duck { public: RedheadDuck(){} virtual ~RedheadDuck(){} public: void Quack(){std::cout << "RedheadDuck:Quack()" << std::endl;} void Swim(){std::cout << "RedheadDuck:Swim()" << std::endl;} void Display(){std::cout << "RedheadDuck:Display()" << std::endl;} void Fly(){std::cout << "RedheadDuck:Fly()" << std::endl;} }; class RubberDuck: public Duck { public: RubberDuck(){} virtual ~RubberDuck(){} public: void Quack(){std::cout << "RubberDuck:Quack()" << std::endl;} void Swim(){std::cout << "RubberDuck:Swim()" << std::endl;} void Display(){std::cout << "RubberDuck:Display()" << std::endl;} void Fly(){} }; */ //策略模式, 每当发生变化时, 只需要修改一下分离出来的变化部分即可 class Flyable { public: Flyable(){} virtual ~Flyable(){}; public: virtual void Fly() = 0; }; class Quackable { public: Quackable(){} virtual ~Quackable(){}; public: virtual void Quack() = 0; }; class Duck { public: Duck(Flyable *lpFlyable = NULL, Quackable *lpQuackable = NULL) { m_lpQuackable = lpQuackable; m_lpFlyable = lpFlyable; } virtual ~Duck(){}; public: virtual void Swim() = 0; virtual void Display() = 0; virtual void SetQuack(Quackable *lpQuackable) { m_lpQuackable = lpQuackable; } virtual void SetFly(Flyable *lpFlyable) { m_lpFlyable = lpFlyable; } virtual void PerFromQuack() { if (m_lpQuackable != NULL) { m_lpQuackable->Quack(); } } virtual void PerFromFly() { if (m_lpFlyable != NULL) { m_lpFlyable->Fly(); } } protected: Flyable *m_lpFlyable; Quackable *m_lpQuackable; }; class FlyWithWing : public Flyable { public: FlyWithWing(){} virtual ~FlyWithWing(){}; virtual void Fly() { std::cout << "I am Flying...." << std::endl; } }; class FlyNoWay : public Flyable { public: FlyNoWay(){} virtual ~FlyNoWay(){}; virtual void Fly() { std::cout << "I am No Flying...." << std::endl; } }; class MallardDuck: public Duck { public: MallardDuck(Flyable *lpFlyable = NULL, Quackable *lpQuackable = NULL) { m_lpFlyable = lpFlyable; m_lpQuackable = lpQuackable; } virtual ~MallardDuck(){} public: void Swim(){std::cout << "MallardDuck:Swim()" << std::endl;} void Display(){std::cout << "MallardDuck:Display()" << std::endl;} }; class RedheadDuck: public Duck { public: RedheadDuck(Flyable *lpFlyable = NULL, Quackable *lpQuackable = NULL) { m_lpFlyable = lpFlyable; m_lpQuackable = lpQuackable; } virtual ~RedheadDuck(){} public: void Swim(){std::cout << "RedheadDuck:Swim()" << std::endl;} void Display(){std::cout << "RedheadDuck:Display()" << std::endl;} }; class RubberDuck: public Duck { public: RubberDuck(Flyable *lpFlyable = NULL, Quackable *lpQuackable = NULL) { m_lpFlyable = lpFlyable; m_lpQuackable = lpQuackable; } virtual ~RubberDuck(){} public: void Swim(){std::cout << "RubberDuck:Swim()" << std::endl;} void Display(){std::cout << "RubberDuck:Display()" << std::endl;} }; void fun(Duck *lpDuck) { lpDuck->PerFromQuack(); lpDuck->Display(); lpDuck->Swim(); lpDuck->PerFromFly(); } int main(int argc, char* argv[]) { //由于是例子, 这里就不考虑资源释放的问题了 fun(new RubberDuck(new FlyNoWay())); fun(new MallardDuck(new FlyWithWing())); fun(new RedheadDuck(new FlyWithWing())); return 0; }
标签:
原文地址:http://www.cnblogs.com/dacainiao/p/5400724.html