ccplusplus.com
Learn C, C++ Concepts
Wednesday, January 4, 2012
state pattern example code
/******************************************************************* * File : state-pattern.cpp * Author : Saurabh Gupta * Description : state design pattern c++ * State is like an enum, but we can define different * behaviour for each state. We could add another class, * Sad, which did something entirely different when asked to m_talk(). * Date : PM 07:47 04 January 2012 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : *******************************************************************/ #include <iostream> using namespace std; class CBehaviour { public: virtual ~CBehaviour() { } virtual void m_talk()=0; }; class CFriendly : public CBehaviour { public: void m_talk() { cout << "www.ccplusplus.com" << endl; } }; void display_message(CBehaviour & mood){ mood.m_talk(); } int main(){ CFriendly mood; display_message(mood); return 0; } /* * OUTPUT * [sgupta@rhel55x86 state]$ c++ state-pattern.cpp -o state-pattern [sgupta@rhel55x86 state]$ ./state-pattern www.ccplusplus.com [sgupta@rhel55x86 state]$ */
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