ccplusplus.com
Learn C, C++ Concepts
Thursday, January 5, 2012
strategy pattern example in c++
/******************************************************************* * File : strategy-pattern.cpp * Author : Saurabh Gupta * Description : strategy pattern example in c++ * Strategy is an algorithm that you pass into a * function or class that uses the strategy. The * idea is that you can provide a different strategy * to achieve something different. * Date : PM 08:11 05 January 2012 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : *******************************************************************/ #include <iostream> #include <string> using namespace std; class CStrategyPattern { public: virtual ~CStrategyPattern() { } virtual string m_format(const string &, const string &) const=0; }; class Formatter : public CStrategyPattern { public: string m_format(const string & s1, const string & s2) const { return s1 + "" + s2 + ""; } }; void display_message(const CStrategyPattern & strategy){ cout << strategy.m_format("c", "cplusplus.com") << endl; } int main() { display_message(Formatter()); return 0; } /* * OUTPUT * [sgupta@rhel55x86 strategy]$ c++ strategy-pattern.cpp -o strategy-pattern [sgupta@rhel55x86 strategy]$ ./strategy-pattern ccplusplus.com [sgupta@rhel55x86 strategy]$ */
See Also:
Design pattern Concept and Sample Codes
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment