Saturday, June 23, 2012

Visitor design pattern sample in C++


Visitor design pattern represent an operation to be performed on the elements of an object structure.
Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Using visitor you put all operations to be performed on various elements of an object to a single separate class names "Visitor".

Here I'm publishing a sample code to demonstrate how to use this design pattern to in marketing solution.
Here we have a portfolio of products ( portfolio has many elements/products ) and every product has a method named "Accept" which accepts a visitor and calls it's Visit(..) function by sending itself as argument to it.
Please see following code for more detailed information :
----------------------
#ifndef _VISITOR_H
#define _VISITOR_H
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>
using namespace std;
class Tableau;
class KaraPhone;
class  ProductPortfo;
class ProductBase;
class ProductVisitor
{
public:
 virtual void Visit(Tableau* )=0;
 virtual void Visit(KaraPhone* )=0;
 virtual void Visit(ProductPortfo* )=0;
};
class ProductBase
{
public:
 virtual void Accept(class ProductVisitor* )=0;
 string Name;
};
class Tableau:public ProductBase
{
private:
 Tableau(){}
public :
 Tableau(string pName){Name=pName;}
 void Accept(ProductVisitor* pVisitor)
 {
  pVisitor->Visit(this);
 }
 
};
class KaraPhone:public ProductBase
{
private:
 KaraPhone(){}
public :
 KaraPhone(string pName){Name=pName;}
 void Accept(ProductVisitor* pVisitor)
 {
  pVisitor->Visit(this);
 }
};
class ProductPortfo:public ProductBase
{
public:
 ProductBase *mProducts[2];
 
public:
 ProductPortfo()
 {
  mProducts[0]= new Tableau("tableau7");
  mProducts[1]= new KaraPhone("Karaphonev1");
  Name="Products Portfo";
 }
 
 void Accept(ProductVisitor *pVisitor)
 {
  for(int i=0;i<sizeof(mProducts)/sizeof(mProducts[0]);i++)
   mProducts[i]->Accept(pVisitor);
  pVisitor->Visit(this);
 }
};
class PresentorVisitor:public ProductVisitor
{
public :
 void Visit(Tableau *pTableau)
 {
  cout << "PRESENT: best BI solution is " << pTableau->Name;
 }
 void Visit(KaraPhone *pKaraPhone)
 {
  cout << "PRESENT: Karaphone helps you to enefit :" << pKaraPhone->Name;
 }
 void Visit(ProductPortfo *pProd)
 {
  cout << "PRESE tell about company products " << pProd->Name;
 }
};
class POCVisitor:public ProductVisitor
{
public :
 void Visit(Tableau *pTableau)
 {
  cout << " POC: this dashboard is made by " << pTableau->Name;
 }
 void Visit(KaraPhone *pKaraPhone)
 {
  cout << "POC: this benefit is after running this :" << pKaraPhone->Name;
 }
 void Visit(ProductPortfo *pProd)
 {
  cout << "Give  CD of demo softwares :" << pProd->Name;
 }
};

#endif


No comments :

Post a Comment

Saturday, June 23, 2012

Visitor design pattern sample in C++


Visitor design pattern represent an operation to be performed on the elements of an object structure.
Visitor lets you define a new operation without changing the classes of the elements on which it operates.
Using visitor you put all operations to be performed on various elements of an object to a single separate class names "Visitor".

Here I'm publishing a sample code to demonstrate how to use this design pattern to in marketing solution.
Here we have a portfolio of products ( portfolio has many elements/products ) and every product has a method named "Accept" which accepts a visitor and calls it's Visit(..) function by sending itself as argument to it.
Please see following code for more detailed information :
----------------------
#ifndef _VISITOR_H
#define _VISITOR_H
#include <string>
#include <iostream>
#include <vector>
#include <iterator>
#include <algorithm>
#include <list>
using namespace std;
class Tableau;
class KaraPhone;
class  ProductPortfo;
class ProductBase;
class ProductVisitor
{
public:
 virtual void Visit(Tableau* )=0;
 virtual void Visit(KaraPhone* )=0;
 virtual void Visit(ProductPortfo* )=0;
};
class ProductBase
{
public:
 virtual void Accept(class ProductVisitor* )=0;
 string Name;
};
class Tableau:public ProductBase
{
private:
 Tableau(){}
public :
 Tableau(string pName){Name=pName;}
 void Accept(ProductVisitor* pVisitor)
 {
  pVisitor->Visit(this);
 }
 
};
class KaraPhone:public ProductBase
{
private:
 KaraPhone(){}
public :
 KaraPhone(string pName){Name=pName;}
 void Accept(ProductVisitor* pVisitor)
 {
  pVisitor->Visit(this);
 }
};
class ProductPortfo:public ProductBase
{
public:
 ProductBase *mProducts[2];
 
public:
 ProductPortfo()
 {
  mProducts[0]= new Tableau("tableau7");
  mProducts[1]= new KaraPhone("Karaphonev1");
  Name="Products Portfo";
 }
 
 void Accept(ProductVisitor *pVisitor)
 {
  for(int i=0;i<sizeof(mProducts)/sizeof(mProducts[0]);i++)
   mProducts[i]->Accept(pVisitor);
  pVisitor->Visit(this);
 }
};
class PresentorVisitor:public ProductVisitor
{
public :
 void Visit(Tableau *pTableau)
 {
  cout << "PRESENT: best BI solution is " << pTableau->Name;
 }
 void Visit(KaraPhone *pKaraPhone)
 {
  cout << "PRESENT: Karaphone helps you to enefit :" << pKaraPhone->Name;
 }
 void Visit(ProductPortfo *pProd)
 {
  cout << "PRESE tell about company products " << pProd->Name;
 }
};
class POCVisitor:public ProductVisitor
{
public :
 void Visit(Tableau *pTableau)
 {
  cout << " POC: this dashboard is made by " << pTableau->Name;
 }
 void Visit(KaraPhone *pKaraPhone)
 {
  cout << "POC: this benefit is after running this :" << pKaraPhone->Name;
 }
 void Visit(ProductPortfo *pProd)
 {
  cout << "Give  CD of demo softwares :" << pProd->Name;
 }
};

#endif


No comments :

Post a Comment