[Home]
[Edit this page]
[Recent Changes]
[Special Pages]
[Help]
CppStrategy
A Strategy encapsulates an algorithm that is used by the Context class. The Context can change its Strategy for doing this algorithm.
A Strategy is like the State Design Pattern, except that State also has a pointer to its Context.
Below is an example of a Context class having two 'Strategies' for its two member variables. These 'Strategies' are either adding or substracting them.
[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
CppStrategy
(C++) Strategy
A Design Pattern (see reference 1).A Strategy encapsulates an algorithm that is used by the Context class. The Context can change its Strategy for doing this algorithm.
A Strategy is like the State Design Pattern, except that State also has a pointer to its Context.
Below is an example of a Context class having two 'Strategies' for its two member variables. These 'Strategies' are either adding or substracting them.
enum enumStrategy{ plus, minus}; //--------------------------------------------------------------------------- // CLASS DECLARATION // (for in .H file) //--------------------------------------------------------------------------- class StrategyBase; //Forward declaration class Context { public: Context(); void setStrategy(const enumStrategy&); void doIt() const; private: std::auto_ptr<StrategyBase> mStrategy; const int mValue1; const int mValue2; }; class StrategyBase { public: StrategyBase() {} //Empty constructor virtual int doIt(const int&, const int&) const = 0; }; class StrategyPlus : public StrategyBase { public: StrategyPlus() {} //Empty constructor int doIt(const int&, const int&) const; }; class StrategyMinus : public StrategyBase { public: StrategyMinus() {} //Empty constructor int doIt(const int&, const int&) const; }; //--------------------------------------------------------------------------- // CLASS DEFINITION // (for in .CPP file) //--------------------------------------------------------------------------- Context::Context() : mStrategy(new StrategyPlus), //Initializes Strategy mValue1(std::rand()%100), //Random number mValue2(std::rand()%100) //Random number { //Nothing } //--------------------------------------------------------------------------- void Context::setStrategy(const enumStrategy& state) { switch(state) { case plus : mStrategy.reset(new StrategyPlus); break; case minus: mStrategy.reset(new StrategyMinus); break; default: assert(!"Unknown state"); std::exit(1); //Use of assert to get line number } } //--------------------------------------------------------------------------- void Context::doIt() const { //The function doIt is delegated to the Strategy //Therefore this function is Strategy-dependent std::cout << "Doing it with " << mValue1 << " and " << mValue2 << ": " << mStrategy->doIt(mValue1,mValue2) << std::endl; } //--------------------------------------------------------------------------- int StrategyPlus::doIt(const int& value1, const int& value2) const { //Adds the two values return (value1 + value2); } //--------------------------------------------------------------------------- int StrategyMinus::doIt(const int& value1, const int& value2) const { //Substracts the two values return (value1 - value2); } //--------------------------------------------------------------------------- int main() { //Make a Context const std::auto_ptr<Context> myContext(new Context); //Set the Strategy myContext->setStrategy(plus); //Do the Strategy-dependent function myContext->doIt(); //Set the Strategy myContext->setStrategy(minus); //Do the Strategy-dependent function myContext->doIt(); //Program done }
- include <iostream>
- include <memory>
- include <cassert>
The Strategy in other programming languages
- (not yet on CodePedia)
Topic links
- #include
- scope operator'::'
- stream out operator '<<'
- assert
- auto_ptr
- class
- char
- const
- cout
- endl
- enum
- friend
- iostream
- #include
- int
- main
- operator<<
- operator::
- public
- return
- std
- std::auto_ptr
- std::cout
- std::endl
References
1) Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Design Patterns. Publisher: Addison-Wesley Professional; 1st edition (January 15, 1995). ISBN: 0201633612[Edit this page] [Page history] [What links here] [Discuss this topic] [Printer Friendly]
