ccplusplus.com
Learn C, C++ Concepts
Tuesday, December 27, 2011
flyweight design pattern example
/******************************************************************* * File : flyweight-pattern.cpp * Author : Saurabh Gupta * Description : flyweight design pattern c++ * Date : PM 10:33 27 December 2011 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : ******************************************************************/ #include <iostream> #include <string> #include <algorithm> using namespace std; class CFlyWeight { private: char m_cCh; public: CFlyWeight(char c) : m_cCh(c) { } void m_OutPut() const { cout << m_cCh; } }; struct OutputChar { void operator()(char m_cCh) const { CFlyWeight(m_cCh).m_OutPut(); } }; void helloworld(const string & message) { for_each(message.begin(), message.end(), OutputChar()); } int main() { helloworld("Hello world!\n"); return 0; } /* * OUTPUT * [sgupta@rhel6x64 flyweight]$ c++ flyweight-pattern.cpp -o flyweight-pattern [sgupta@rhel6x64 flyweight]$ ./flyweight-pattern Hello world! [sgupta@rhel6x64 flyweight]$ */
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