首页 > 代码库 > 访问者模式之C++实现
访问者模式之C++实现
#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Visitor;
class Element
{
protected:
string name;
public:
Element(string name)
{
this->name = name;
}
string GetName() const
{
return this->name;
}
virtual void Accept(Visitor *visitor) = 0;
};
class Visitor
{
public:
virtual void Visit(Element* element) = 0;
};
class Friend : public Visitor
{
public:
void Visit(Element* element)
{
cout << "好友访问" << element->GetName() << endl;
}
};
class Stranger : public Visitor
{
public:
void Visit(Element* element)
{
cout << "陌生人访问" << element->GetName() << endl;
}
};
class Zoom : public Element
{
public:
Zoom(string name) : Element(name) {}
virtual void Accept(Visitor *visitor)
{
visitor->Visit(this);
}
};
class Article
{
private:
vector<Element*> vec_element;
public:
void AddElement(Element* element)
{
vec_element.push_back(element);
}
void RemoveElement(Element* element)
{
for (vector<Element*>::iterator it = vec_element.begin(); it != vec_element.end(); )
{
if (*it == element)
{
vec_element.erase(it);
break;
}
it++;
}
}
void Accept(Visitor *visitor)
{
Element *element = NULL;
for (vector<Element*>::iterator it = vec_element.begin(); it != vec_element.end(); it++)
{
element = *it;
element->Accept(visitor);
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Article *pArticles = new Article;
pArticles->AddElement(new Zoom("文章1"));
pArticles->AddElement(new Zoom("文章2"));
pArticles->AddElement(new Zoom("文章3"));
Visitor *pFriend = new Friend;
Visitor *pStranger = new Stranger;
pArticles->Accept(pFriend);
pArticles->Accept(pStranger);
return 0;
}
#include <iostream>
#include <vector>
#include <string>
using namespace std;
class Visitor;
class Element
{
protected:
string name;
public:
Element(string name)
{
this->name = name;
}
string GetName() const
{
return this->name;
}
virtual void Accept(Visitor *visitor) = 0;
};
class Visitor
{
public:
virtual void Visit(Element* element) = 0;
};
class Friend : public Visitor
{
public:
void Visit(Element* element)
{
cout << "好友访问" << element->GetName() << endl;
}
};
class Stranger : public Visitor
{
public:
void Visit(Element* element)
{
cout << "陌生人访问" << element->GetName() << endl;
}
};
class Zoom : public Element
{
public:
Zoom(string name) : Element(name) {}
virtual void Accept(Visitor *visitor)
{
visitor->Visit(this);
}
};
class Article
{
private:
vector<Element*> vec_element;
public:
void AddElement(Element* element)
{
vec_element.push_back(element);
}
void RemoveElement(Element* element)
{
for (vector<Element*>::iterator it = vec_element.begin(); it != vec_element.end(); )
{
if (*it == element)
{
vec_element.erase(it);
break;
}
it++;
}
}
void Accept(Visitor *visitor)
{
Element *element = NULL;
for (vector<Element*>::iterator it = vec_element.begin(); it != vec_element.end(); it++)
{
element = *it;
element->Accept(visitor);
}
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Article *pArticles = new Article;
pArticles->AddElement(new Zoom("文章1"));
pArticles->AddElement(new Zoom("文章2"));
pArticles->AddElement(new Zoom("文章3"));
Visitor *pFriend = new Friend;
Visitor *pStranger = new Stranger;
pArticles->Accept(pFriend);
pArticles->Accept(pStranger);
return 0;
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。