Visitor 访问者

#include <cstdlib>
#include <iostream>

using namespace std;
class Visitor
{
public:
  virtual void VisitConcreteA() = 0;
  virtual void VisitConcreteB() = 0;
  virtual void VisitConcreteC() = 0;
};
class VisitorA:public Visitor
{
public:
  virtual void VisitConcreteA(){cout<<"VisitorA 准备采访ConcreteA ";}
  virtual void VisitConcreteB(){cout<<"VisitorA 准备采访ConcreteB ";}
  virtual void VisitConcreteC(){cout<<"VisitorA 准备采访ConcreteC ";}
};
class VisitorB:public Visitor
{
public:
  virtual void VisitConcreteA(){cout<<"VisitorB 准备采访ConcreteA ";}
  virtual void VisitConcreteB(){cout<<"VisitorB 准备采访ConcreteB ";}
  virtual void VisitConcreteC(){cout<<"VisitorB 准备采访ConcreteC ";}
};
class VisitorC:public Visitor
{
public:
  virtual void VisitConcreteA(){cout<<"VisitorC 准备采访ConcreteA ";}
  virtual void VisitConcreteB(){cout<<"VisitorC 准备采访ConcreteB ";}
  virtual void VisitConcreteC(){cout<<"VisitorC 准备采访ConcreteC ";}
};
class Concrete
{
public:
  virtual void Accept(Visitor* pVis) = 0;
};
class ConcreteA:public Concrete
{
public:
  virtual void Accept(Visitor* pVis){pVis->VisitConcreteA();cout<<"ConcreteA 接受访问"<<endl;}
};
class ConcreteB:public Concrete
{
public:
  virtual void Accept(Visitor* pVis){pVis->VisitConcreteB();cout<<"ConcreteB 接受访问"<<endl;}
};
class ConcreteC:public Concrete
{
public:
  virtual void Accept(Visitor* pVis){pVis->VisitConcreteC();cout<<"ConcreteC 接受访问"<<endl;}
};
void Do(Visitor* pVis,Concrete* pCon)
{
  pCon->Accept(pVis);
  delete pVis;
  delete pCon;
}
int main(int argc, char *argv[])
{
    Do(new VisitorA,new ConcreteA);
    Do(new VisitorA,new ConcreteB);
    Do(new VisitorA,new ConcreteC);
    cout<<"----------"<<endl;
    Do(new VisitorB,new ConcreteA);
    Do(new VisitorB,new ConcreteB);
    Do(new VisitorB,new ConcreteC);
    cout<<"----------"<<endl;
    Do(new VisitorC,new ConcreteA);
    Do(new VisitorC,new ConcreteB);
    Do(new VisitorC,new ConcreteC);
    cout<<"----------"<<endl;
    system("PAUSE");
    return EXIT_SUCCESS;
}

你可能感兴趣的:(Visitor 访问者)