/** * strategy.h */ /** * This class declares an interface common to all supported * algorithms. Context uses this interface to call the algorithm * defined by a ConcreteStrategy object. */ class Strategy { public: virtual void AlgorithmInterface() = 0; }; /** * This class implements an algorithm using the Strategy interface. */ class ConcreteStrategyA: public Strategy { public: virtual void AlgorithmInterface() { // Do something unique. }; }; /** * This class implements an algorithm using the Strategy interface. */ class ConcreteStrategyB: public Strategy { public: virtual void AlgorithmInterface() { // Do something unique. }; }; /** * This class implements an algorithm using the Strategy interface. */ class ConcreteStrategyC: public Strategy { public: virtual void AlgorithmInterface() { // Do something unique. }; }; /** * This class is configured with a ConcreteStrategy * object. It maintains a reference to a Strategy object. * This class may define an interface that lets Strategy * access its data. */ class Context { private: /** * Encapsulated instance of a strategy class. */ Strategy* strategy; public: /** * Default constructor. */ Context() { strategy = (Strategy *) 0; }; /** * This method is the one which calls the strategy * object to perform some work. */ public: void Operation() { strategy -> AlgorithmInterface(); }; public: /** * Accessors for the strategy relationship. */ Strategy* getStrategy() { return strategy; }; void setStrategy( Strategy* aStrategy ) { strategy = aStrategy; }; };