ccplusplus.com
Learn C, C++ Concepts
Wednesday, January 4, 2012
prototype pattern c++ example
/*************************************************************************************** * File : prototype-pattern.cpp * Author : Saurabh Gupta * Description : prototype design pattern example c++ * A Prototype is an object which is cloneable, i.e. you can create a * copy, even though you don't know what you are creating a copy of. * Date : AM 11:13 04 january 2012 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : ***************************************************************************************/ #include <iostream> #include <memory> class CPrototypeOperation { public: virtual ~CPrototypeOperation() { } virtual std::auto_ptr<CPrototypeOperation> clone() const=0; virtual void m_Display()=0; }; class CDisplayMessage : public CPrototypeOperation { public: std::auto_ptr<CPrototypeOperation> clone() const { return std::auto_ptr<CPrototypeOperation>(new CDisplayMessage); } void m_Display() { std::cout << "www.ccplusplus.com" << std::endl; } }; void display_message(const CPrototypeOperation & something) { std::auto_ptr<CPrototypeOperation> clone = something.clone(); clone->m_Display(); } int main() { display_message(CDisplayMessage()); return 0; } /* * OUTPUT * [sgupta@rhel6x64 prototype]$ c++ prototype-pattern.cpp -o prototype-pattern [sgupta@rhel6x64 prototype]$ ./prototype-pattern www.ccplusplus.com [sgupta@rhel6x64 prototype]$ */
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