#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);
}
访问者模式
最新推荐文章于 2024-10-02 21:12:17 发布