设计模式 - Strategy
意图:定义一系列的算法,把它们一个个封装起来, 并且使它们可相互替换。本模式使得算法可独立于使用它的客户而变化。
class Strategy { public: Strategy() {} virtual ~Strategy() {} virtual void AlgorithmInterface() = 0; }; class ConcreteStrategyA: public Strategy { public: ConcreteStrategyA() {} ~ConcreteStrategyA() {} void AlgorithmInterface() { cout<<"ConcreteStrategyA::AlgorithmInterface()"<<endl; } }; class ConcreteStrategyB: public Strategy { public: ConcreteStrategyB() {} ~ConcreteStrategyB() {} void AlgorithmInterface() { cout<<"ConcreteStrategyB::AlgorithmInterface()"<<endl; } }; class Context { public: Context(Strategy* strategy) { this->strategy = strategy; } ~Context() {} void DoAction() { strategy->AlgorithmInterface(); } private: Strategy* strategy; };