ccplusplus.com
Learn C, C++ Concepts
Thursday, December 22, 2011
composite design pattern example
/******************************************************************* * File : composite-pattern.cpp * Author : Saurabh Gupta * Description : composite design pattern example c++ * Date : PM 09:56 22 December 2011 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : ******************************************************************/ #include <iostream> #include <string> class CGreeting { public: virtual ~CGreeting() { } virtual std::string m_getGreeting()=0; }; class CRecipient { public: virtual ~CRecipient() { } virtual std::string m_getRecipient()=0; }; class CComposite { private: CGreeting & greeting; CRecipient & recipient; public: CComposite(CGreeting & greeting, CRecipient & recipient) : greeting(greeting), recipient(recipient) { } void greet() const { std::cout << greeting.m_getGreeting() << " " << recipient.m_getRecipient() << "!" << std::endl; } }; class CHello : public CGreeting { public: std::string m_getGreeting() { return "CHello"; } }; class CWorld : public CRecipient { public: std::string m_getRecipient() { return "world"; } }; void HelloWorld(const CComposite & composite) { composite.greet(); } int main() { CHello hello; CWorld world; HelloWorld(CComposite(hello, world)); return 0; } /* * OUTPUT * [sgupta@rhel6x64 composite]$ c++ composite-pattern.cpp -o composite-pattern [sgupta@rhel6x64 composite]$ ./composite-pattern CHello world! [sgupta@rhel6x64 composite]$ */
See Also:
Design Pattern Concepts and Example C++ Codes
1 comment:
andy bratt
December 28, 2011 at 10:44 AM
Nice example. well explained . thanks.
Andy.
Reply
Delete
Replies
Reply
Add comment
Load more...
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
Nice example. well explained . thanks.
ReplyDeleteAndy.