访问者模式

#include "stdafx.h"                                                                                                  -          =\00
#include <list>
#include <string>
using namespace std;

class Element;
class Element1;
class Element2;
class Element3;
//访问者抽象类
class Visitor
{
public:
	string m_strName;
public:
	//访问元素
	virtual void visitElement1(Element1 *pElement1){};
	virtual void visitElement2(Element2 *pElement2){};
	virtual void visitElement3(Element3 *pElement3){};
};
//元素抽象类
class Element
{
public:
	string m_strName;
public:
	virtual void acceptVisitor(Visitor *pVisitor){};
};

class Element1 : public Element
{
public:
	
	Element1(string strName)//不能用初始化参数列表
	{
		this->m_strName = strName;
	}
public:
	void acceptVisitor(Visitor *pVisitor)
	{
		pVisitor->visitElement1(this);
	}
};


class Element2 : public Element
{
public:
	Element2(string strName)
	{
		this->m_strName = strName;
	}
public:
	void acceptVisitor(Visitor *pVisitor)
	{
		pVisitor->visitElement2(this);
	}
};

class Element3 : public Element
{
public:
	Element3(string strName)
	{
		this->m_strName = strName;
	}
public:
	void acceptVisitor(Visitor *pVisitor)
	{
		pVisitor->visitElement3(this);
	}
};

class Visitor1 : public Visitor
{
public:
	Visitor1(string strName)
	{
		this->m_strName = strName;
	}
public:
	void visitElement1(Element1 *pElement1)
	{
		//访问者1处理了元素1的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement1->m_strName << " element." << endl;
	}

	void visitElement2(Element2 *pElement2)
	{
		//访问者1处理了元素2的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement2->m_strName << " element." << endl;
	}
	void visitElement3(Element3 *pElement3)
	{
		//访问者3处理了元素2的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement3->m_strName << " element." << endl;
	}

};

class Visitor2 : public Visitor
{
public:
	Visitor2(string strName)
	{
		this->m_strName = strName;
	}
public:
	void visitElement1(Element1 *pElement1)
	{
		//访问者1处理了元素1的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement1->m_strName << " element." << endl;
	}

	void visitElement2(Element2 *pElement2)
	{
		//访问者1处理了元素2的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement2->m_strName << " element." << endl;
	}
	void visitElement3(Element3 *pElement3)
	{
		//访问者3处理了元素2的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement3->m_strName << " element." << endl;
	}

};

class Visitor3 : public Visitor
{
public:
	Visitor3(string strName)
	{
		this->m_strName = strName;
	}
public:
	void visitElement1(Element1 *pElement1)
	{
		//访问者1处理了元素1的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement1->m_strName << " element." << endl;
	}

	void visitElement2(Element2 *pElement2)
	{
		//访问者1处理了元素2的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement2->m_strName << " element." << endl;
	}
	void visitElement3(Element3 *pElement3)
	{
		//访问者3处理了元素2的功能函数
		cout << "Visitor Name:" << m_strName << " handled " << pElement3->m_strName << " element." << endl;
	}
};
//元素的集合
class Elements
{
public:
	list<Element*> m_listElements;
public:
	void addElement(Element* pElement)
	{
		m_listElements.push_back(pElement);
	}
	void removeElement(Element *pElement)
	{
		m_listElements.remove(pElement);
	}
	void acceptVisitor(Visitor *pVisitor)
	{
		for(list<Element*>::iterator it = m_listElements.begin();it != m_listElements.end();++it)
		{
			(*it)->acceptVisitor(pVisitor);
		}
	}

};

void testVisitorMode3()
{
	Visitor1 *pVisitor1 = new Visitor1("VisitorA");
	Visitor2 *pVisitor2 = new Visitor2("VisitorB");
	Visitor3 *pVisitor3 = new Visitor3("VisitorC");
	Element1 *pElement1 = new Element1("Element1");
	Element2 *pElement2 = new Element2("Element2");
	Element3 *pElement3 = new Element3("Element3");
	Elements *pElements = new Elements();
	pElements->addElement(pElement1);
	pElements->addElement(pElement2);
	pElements->addElement(pElement3);
	pElements->acceptVisitor(pVisitor1);
	pElements->acceptVisitor(pVisitor2);
	pElements->acceptVisitor(pVisitor3);

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值