ccplusplus.com
Learn C, C++ Concepts
Friday, December 23, 2011
decorator design pattern c++ example
/******************************************************************* * File : decorator-pattern.cpp * Author : Saurabh Gupta * Description : decorator design pattern example c++ * Date : AM 09:13 23 December 2011 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : *******************************************************************/ #include <iostream> #include <string> using namespace std; class CGreeting { public: virtual ~CGreeting() { } virtual string m_getGreeting() const=0; }; class CExclamation : public CGreeting { private: const CGreeting & greeting; public: CExclamation(const CGreeting & gr) : greeting(gr) { } string m_getGreeting() const { return greeting.m_getGreeting() + "!"; } }; class CHelloWorld : public CGreeting { public: string m_getGreeting() const { return "Hello world"; } }; void helloworld(const CGreeting & greeting) { cout << greeting.m_getGreeting() << endl; } int main() { CHelloWorld hw; helloworld(CExclamation(hw)); return 0; } /* * OUTPUT * -rw-rw-r--. 1 sgupta sgupta 637 Dec 23 08:57 decorator-pattern.cpp [sgupta@rhel6x64 decorator]$ c++ decorator-pattern.cpp -o decorator-pattern [sgupta@rhel6x64 decorator]$ ./decorator-pattern Hello world! [sgupta@rhel6x64 decorator]$ */
See Also:
Design Pattern Concepts and Example C++ Codes
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment