ccplusplus.com
Learn C, C++ Concepts
Monday, January 9, 2012
visitor design pattern c++ example
/*************************************************************************************** * File : visitor-pattern.cpp * Author : Saurabh Gupta * Description : visitor design pattern c++ example * visitor Pattern provides a call-back for every item in a collection. * This allows the implementation of the collection to hidden. * Date : PM 04:05 09 January 2012 * Source : http://www.ccplusplus.com/2011/07/design-pattern-guide-with-example-and.html * Note : ***************************************************************************************/ #include <vector> #include <string> #include <iostream> using namespace std; class CVisitorPattern { public: virtual ~CVisitorPattern() { } virtual void m_atRecipient(const string &)=0; }; class CRecipient { vector<string> recipients; public: void mAddRecipient(const string & recipient) { recipients.push_back(recipient); } void m_visit(CVisitorPattern & visitor) const { for(vector<string>::const_iterator recipient = recipients.begin(); recipient != recipients.end(); ++recipient) { visitor.m_atRecipient(*recipient); } } }; class whereAreWe : public CVisitorPattern { public: void m_atRecipient(const string & recipient) { cout << "www." << recipient << endl; } }; void display_message(CVisitorPattern & visitor) { CRecipient recipients; recipients.mAddRecipient("ccplusplus.com"); recipients.m_visit(visitor); } int main() { whereAreWe visitor; display_message(visitor); return 0; } /* * OUTPUT * [sgupta@rhel6x64 visitor]$ c++ visitor-pattern.cpp -o visitor-pattern [sgupta@rhel6x64 visitor]$ ./visitor-pattern www.ccplusplus.com [sgupta@rhel6x64 visitor]$ */
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