[Home]  [Edit this page]  [Recent Changes]  [Special Pages]  [Help
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.

  1. include <iostream>
  2. include <memory>
  3. include <cassert>
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 }




The Strategy in other programming languages

  • (not yet on CodePedia)

Topic links

References

1) Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides. Design Patterns. Publisher: Addison-Wesley Professional; 1st edition (January 15, 1995). ISBN: 0201633612

last edited (November 18, 2006) by bilderbikkel, Number of views: 3332, Current Rev: 10 (Diff)

[Edit this page]  [Page history]  [What links here]  [Discuss this topic]  [Printer Friendly]  

Members

Username:

Password:


Register
Forgot Password?




Programmers Heaven - for .NET, Java, C/C++ and WEB Developers!
© 1996-2008 Community Networks Ltd. All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Tore Nestenius at .NET Consultant - Synchron Data.