ccplusplus.com
Learn C, C++ Concepts
Saturday, January 14, 2012
adapter design pattern example
/******************************************************************* * File : adapter-design-pattern.cpp * Author : Saurabh Gupta * Description : adapter design pattern example * Convert one class so that it appears to be another class. * Date : PM 02:51 14 January 2012 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : *******************************************************************/ #include <iostream> class CRoundShape { public: virtual ~CRoundShape() { } virtual void m_doRound() const=0; }; class CSquareShape { public: virtual ~CSquareShape() { } virtual void m_doSquare() const=0; }; class CRound_Hole { public: void m_insert(const CRoundShape & shape) const { shape.m_doRound(); } }; class CSquareShapeAdapter : public CRoundShape { private: CSquareShape & square_shape; public: CSquareShapeAdapter(CSquareShape & shape) : square_shape(shape){ } void m_doRound() const { square_shape.m_doSquare(); } }; class CDisplayMessage : public CSquareShape { public: void m_doSquare() const { std::cout << "ccplusplus.com" << std::endl; } }; void display_message(const CRoundShape & shape) { CRound_Hole().m_insert(shape); } int main() { CDisplayMessage shape; display_message(CSquareShapeAdapter(shape)); return 0; } /* * OUTPUT * [sgupta@rhel6x64 adapter]$ c++ adapter-design-pattern.cpp -o adapter-design-pattern [sgupta@rhel6x64 adapter]$ ./adapter-design-pattern ccplusplus.com [sgupta@rhel6x64 adapter]$ */
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