ccplusplus.com
Learn C, C++ Concepts
Saturday, December 24, 2011
factory method design pattern example
/******************************************************************* * File : factory-method.cpp * Author : Saurabh Gupta * Description : factory method in c++ * Date : PM 11:10 24 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 CFactoryMethod { public: CFactoryMethod() { } virtual ~CFactoryMethod() { } virtual string m_getMessage()=0; void m_greet(){ cout << m_getMessage() << endl; } }; class CHelloWorld : public CFactoryMethod { public: string m_getMessage() { return "Hello world!"; } }; void helloworld(CFactoryMethod & greeter) { greeter.m_greet(); } int main() { CHelloWorld greeter; helloworld(greeter); return 0; } /* * OUTPUT * [sgupta@rhel6x64 factory_method]$ c++ factory-method.cpp -o factory-method [sgupta@rhel6x64 factory_method]$ ./factory-method Hello world! [sgupta@rhel6x64 factory_method]$ */
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