ccplusplus.com
Learn C, C++ Concepts
Sunday, January 1, 2012
momento design pattern example
/******************************************************************* * File : momento-pattern.cpp * Author : Saurabh Gupta * Description : momento design pattern c++ * memento stores a previous state or value so * that it can be restored at a later time. * Date : PM 10:53 01 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 CMementoPattern { private: string m_strOldString; public: CMementoPattern(string & str, const string newString) : m_strOldString(str) { str = newString; } void undo(string & str) { str = m_strOldString; } }; void helloworld(CMementoPattern & memento) { string message; memento.undo(message); cout << message << endl; } int main() { string hw("Hello world!"); CMementoPattern mem(hw, "Goodbye, cruel world!"); helloworld(mem); return 0; } /* * OUTPUT * [sgupta@rhel55x86 momemto]$ c++ momento-pattern.cpp -o momento-pattern [sgupta@rhel55x86 momemto]$ ./momento-pattern Hello world! [sgupta@rhel55x86 momemto]$ */
See Also:
Design pattern Concept and Sample Codes
1 comment:
Anonymous
January 4, 2012 at 9:43 AM
Please post an example of prototype and state pattern. thanks in advance.
Reply
Delete
Replies
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
Please post an example of prototype and state pattern. thanks in advance.
ReplyDelete