5.11 访问者模式 visitor

1.意图: 

    封装一些作用于某种数据结构中的各元素的操作,它可以在不改变这个数据结构的前提下定义作用于这些元素的新的操作。 

2.结构图

5.11 访问者模式 visitor_第1张图片

3.源码

//Element.h
#ifndef _ELEMENT_H_
#define _ELEMENT_H_
class Visitor;
class Element
{ 
public: 
	virtual ~Element(); 
	virtual void  Accept(Visitor* vis) = 0; 
protected: 
	Element() ;  
private: 
}; 
class ConcreteElementA:public Element 
{ 
public: 
	ConcreteElementA(); 
	~ConcreteElementA(); 
	void Accept(Visitor*  vis); 
protected: 
private: 
}; 
class ConcreteElementB:public  Element 
{ 
public: 
	ConcreteElementB(); 
	~ConcreteElementB(); 
	void Accept(Visitor*  vis); 
protected: 
private: 
}; 
#endif //~_ELEMENT_H_
//Visitor.h
#ifndef _VISITOR_H_
#define _VISITOR_H_
class ConcreteElementA;
class ConcreteElementB;
class Element;
class Visitor
{
public :
	virtual ~Visitor();
	virtual void
		VisitConcreteElementA(Element*elm)=0;
	virtual void
		VisitConcreteElementB(Element*elm)=0;
protected:
	Visitor();
private:
};
class ConcreteVisitorA:public  Visitor
{
public :
	ConcreteVisitorA();
	virtual ~ConcreteVisitorA();
	virtual void
		VisitConcreteElementA(Element*elm);
	virtual void
		VisitConcreteElementB(Element*elm);
protected:
private:
};
class ConcreteVisitorB:public Visitor
{
public :
	ConcreteVisitorB();
	virtual ~ConcreteVisitorB();
	virtual void
		VisitConcreteElementA(Element*elm);
	virtual void
		VisitConcreteElementB(Element*elm);
protected:
private:
};
#endif//~_VISITOR_H_
//Element.cpp
#include "Element.h"
#include "Visitor.h"
#include <iostream>
using namespace std;
Element::Element()
{
}
Element::~Element()
{
}
void Element::Accept(Visitor*vis)
{
}
ConcreteElementA::ConcreteElementA()
{
}
ConcreteElementA::~ConcreteElementA()
{
}
void ConcreteElementA::Accept(Visitor*vis)
{
	vis->VisitConcreteElementA(this);
	cout<<"visiting ConcreteElementA..."<<endl;
}
ConcreteElementB::ConcreteElementB()
{
}
ConcreteElementB::~ConcreteElementB()
{
}
void ConcreteElementB::Accept(Visitor*vis)
{
	vis->VisitConcreteElementB(this);
	cout<<"visiting ConcreteElementB..."<<endl;	
}
//Visitor. cpp 
#include "Visitor.h"
#include "Element.h" 
#include <iostream> 
using namespace std; 
Visitor::Visitor() 
{ 
} 
Visitor::~Visitor () 
{ 
} 
ConcreteVisitorA::ConcreteVisitorA()
{ 
} 
ConcreteVisitorA::~ConcreteVisitorA()
{ 
} 
void 
ConcreteVisitorA::VisitConcreteElementA(Element*elm)
{ 
	cout<< "i will visit ConcreteElementA..."<<endl; 
} 
void 
ConcreteVisitorA::VisitConcreteElementB(Element*elm)
{ 
	cout<< "i will visit ConcreteElementB..."<<endl;
} 
ConcreteVisitorB::ConcreteVisitorB()
{ 
} 
ConcreteVisitorB::~ConcreteVisitorB()
{ 
} 
void 
ConcreteVisitorB::VisitConcreteElementA(Element*elm)
{ 
	cout<< "i will visitConcreteElementA..."<<endl;
} 
void 
ConcreteVisitorB::VisitConcreteElementB(Element*elm)
{ 
	cout<< "i will visit ConcreteElementB..."<< endl; 
}
#include "Element.h"
#include "Visitor.h"
#include <iostream>
using namespace std;
int main(int argc,char*argv[])
{
	Visitor*vis=new ConcreteVisitorA();
	Element*elm=new ConcreteElementA();
	elm->Accept(vis);
	getchar();
	return 0;
}






你可能感兴趣的:(5.11 访问者模式 visitor)