定义:
定义了算法族,分别封装起来,让它们之间可以互相替换,以此模式让算法的变化独立于使用算法的客户。
UML图:
代码实现:
/************************************************************************/ /* Strategy.h */ /************************************************************************/ /** * @file Strategy.h * @brief Strategy模式的实现代码 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-02-23 */ #ifndef STRATEGY_H_ #define STRATEGY_H_ #include <string> using std::string; class IQuackBehavior { public: virtual string Quacking() = 0; }; class IFlyBehavior { public: virtual string Fly() = 0; }; class MuteQuack : public IQuackBehavior { public: string Quacking(); }; class Quack : public IQuackBehavior { public: string Quacking(); }; class Squeak : public IQuackBehavior { public: string Quacking(); }; class FlyNoWay : public IFlyBehavior { public: string Fly(); }; class FlyRocketPowered : public IFlyBehavior { public: string Fly(); }; class FlyWithWings : public IFlyBehavior { public: string Fly(); }; class Duck { protected: IFlyBehavior* flyBehavior; IQuackBehavior* quackBehavior; public: void SetFly(IFlyBehavior* flyBehavior); void SetQuack(IQuackBehavior* quackBehavior); virtual string Display() = 0; string PerformFly(); string PerformQuack(); string Swim(); }; class MallardDuck : public Duck { public: MallardDuck(); string Display(); }; class ModelDuck : public Duck { public: ModelDuck(); string Display(); }; class RubberDuck : public Duck { public: RubberDuck(); string Display(); }; #endif /************************************************************************/ /* Strategy.cpp */ /************************************************************************/ /** * @file Strategy.cpp * @brief Strategy模式的实现代码 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-02-23 */ #include "Strategy.h" string MuteQuack::Quacking() { return "silence"; } string Quack::Quacking() { return "Quack"; } string Squeak::Quacking() { return "Squeak"; } string FlyNoWay::Fly() { return "I can't fly."; } string FlyRocketPowered::Fly() { return "I'm flying with a rocket!"; } string FlyWithWings::Fly() { return "I'm flying!!"; } void Duck::SetFly(IFlyBehavior *flyBehavior) { this->flyBehavior = flyBehavior; } void Duck::SetQuack(IQuackBehavior* quackBehavior) { this->quackBehavior = quackBehavior; } string Duck::PerformFly() { return flyBehavior->Fly(); } string Duck::PerformQuack() { return quackBehavior->Quacking(); } string Duck::Swim() { return "All ducks float, even decoys!"; } MallardDuck::MallardDuck() { quackBehavior = new Quack(); flyBehavior = new FlyWithWings(); } string MallardDuck::Display() { return "I'm a real Mallard duck"; } ModelDuck::ModelDuck() { quackBehavior = new Quack(); flyBehavior = new FlyNoWay(); } string ModelDuck::Display() { return "I'm a model duck"; } RubberDuck::RubberDuck() { quackBehavior = new Squeak(); flyBehavior = new FlyNoWay(); } string RubberDuck::Display() { return "I'm a rubber duck"; } /************************************************************************/ /* test.cpp */ /************************************************************************/ /** * @file test.cpp * @brief Strategy模式的实现代码 * @author wlq_729@163.com * http://blog.csdn.net/rabbit729 * @version 1.0 * @date 2009-02-23 */ #include "Strategy.h" #include <iostream> using namespace std; void main(void) { Duck* duck = new MallardDuck(); cout<<duck->PerformQuack()<<endl; cout<<duck->PerformFly()<<endl; duck->SetFly(new FlyRocketPowered()); cout<<duck->PerformFly()<<endl; }
程序输出:
QuackI'm flying!!I'm flying with a rocket!请按任意键继续. . .
posted on 2009-02-23 23:09 张云临 阅读(176) 评论(0) 编辑 收藏 举报