ccplusplus.com
Learn C, C++ Concepts
Tuesday, January 10, 2012
abstract design pattern example
/*************************************************************************************** * File : abstract-factory.cpp * Author : Saurabh Gupta * Description : abstract design pattern example c++ example * : An abstract factory is used to create objects. However you want to * change what object is created. * Date : AM 09:13 04 january 2012 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : ***************************************************************************************/ #include <iostream> #include <string> #include <memory> using namespace std; class CMessage { public: virtual ~CMessage() { } virtual string m_getMessage()=0; }; class CAbstractFactoryMessage { public: virtual ~CAbstractFactoryMessage() { } virtual auto_ptr<CMessage> m_createMessage() const=0; }; class CDisplayMessage : public CMessage { public: string m_getMessage() { return "www.ccplusplus.com"; } }; class CDisplayMessageFactory : public CAbstractFactoryMessage { public: std::auto_ptr<CMessage> m_createMessage() const { return std::auto_ptr<CMessage>(new CDisplayMessage()); } }; void display_message(const CAbstractFactoryMessage & factory) { auto_ptr<CMessage> message = factory.m_createMessage(); cout << message->m_getMessage() << std::endl; } int main() { display_message(CDisplayMessageFactory()); return 0; } /* * OUTPUT * [sgupta@rhel55x86 design-pattern]$ c++ abstract-factory.cpp -o abstract-factory [sgupta@rhel55x86 design-pattern]$ ./abstract-factory www.ccplusplus.com [sgupta@rhel55x86 design-pattern]$ */
Note:
display-message.cpp that is used to explain the design pattern
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