Visitor
#include <iostream>
#include <vector> using namespace std; #define DESTROY_POINTER(ptr) if (ptr) { delete ptr; ptr = NULL; } class Element; class Visitor
{
public:
virtual void Visit(Element* pElement)=;
}; class ConcreteVisitorA : public Visitor
{
public:
void Visit(Element* pElement);
}; class ConcreteVisitorB : public Visitor
{
public:
void Visit(Element* pElement);
}; class Element
{
public:
virtual void Accept(Visitor* pVisitor)=;
virtual void Interface1()=;
virtual void Interface2()=;
}; class ConcreteElementA : public Element
{
public:
void Accept(Visitor* pVisitor)
{
pVisitor->Visit(this);
}
void Interface1() { cout<<"ConcreteElementA::Interface1"<<endl; }
void Interface2() { cout<<"ConcreteElementA::Interface2"<<endl; }
}; class ConcreteElementB : public Element
{
public:
void Accept(Visitor* pVisitor)
{
pVisitor->Visit(this);
}
void Interface1() { cout<<"ConcreteElementB::Interface1"<<endl; }
void Interface2() { cout<<"ConcreteElementB::Interface2"<<endl; }
}; void ConcreteVisitorA::Visit(Element* pElement)
{
pElement->Interface1();
} void ConcreteVisitorB::Visit(Element* pElement)
{
pElement->Interface2();
} class Structure
{
public:
virtual ~Structure()
{
for (unsigned int i = ; i < m_elementList.size(); i++)
{
delete m_elementList[i];
}
m_elementList.clear();
} void Accept(Visitor* pVisitor)
{
for (unsigned int i = ; i < m_elementList.size(); i++)
{
m_elementList[i]->Accept(pVisitor);
}
} void AddElement(Element* pElement)
{
m_elementList.push_back(pElement);
}
private:
vector<Element*> m_elementList;
}; int main(int argc, char *argv[])
{
Structure structure; structure.AddElement(new ConcreteElementA);
structure.AddElement(new ConcreteElementB); Visitor* pVisitorA = new ConcreteVisitorA;
Visitor* pVisitorB = new ConcreteVisitorB; structure.Accept(pVisitorA);
structure.Accept(pVisitorB); DESTROY_POINTER(pVisitorA);
DESTROY_POINTER(pVisitorB); return ;
}
Visitor的更多相关文章
- 完成C++不能做到的事 - Visitor模式
拿着刚磨好的热咖啡,我坐在了显示器前.“美好的一天又开始了”,我想. 昨晚做完了一个非常困难的任务并送给美国同事Review,因此今天只需要根据他们提出的意见适当修改代码并提交,一周的任务就完成了.剩 ...
- 十一个行为模式之访问者模式(Visitor Pattern)
定义: 提供一个作用于某对象结构(通常是一个对象集合)的操作的接口,使得在添加新的操作或者在添加新的元素时,不需要修改原有系统,就可以对各个对象进行操作. 结构图: Visitor:抽象访问者类,对元 ...
- 设计模式之observer and visitor
很长时间一直对observer(观察者)与visitor(访问者)有些分不清晰. 今天有时间进行一下梳理: 1.observer模式 这基本就是一个通知模式,当被观察者发生改变时,通知所有监听此变化的 ...
- 访问者模式(Visitor Pattern)
定义:封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作. Visitor 抽象访问者角色:为该对象结构中具体元素角色声明一个访问操作接口.该操作接口 ...
- 设计模式之美:Visitor(访问者)
索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Visitor 模式结构样式代码. 实现方式(二):使用 Visitor 模式解构设计. 实现方式(三):使用 Acyclic ...
- 访问者(Visitor)模式
http://www.cnblogs.com/zhenyulu/articles/79719.html 一. 访问者(Visitor)模式 访问者模式的目的是封装一些施加于某种数据结构元素之上的操作. ...
- 深入浅出设计模式——访问者模式(Visitor Pattern)
模式动机 对于系统中的某些对象,它们存储在同一个集合中,且具有不同的类型,而且对于该集合中的对象,可以接受一类称为访问者的对象来访问,而且不同的访问者其访问方式有所不同,访问者模式为解决这类问题而诞生 ...
- 无环的visitor模式
无环的访问者模式,是来改进原有访问者模式的不足之处的,是Robert C. Martin首次提出的.我们知道访问者模式的优点是为被访问继承体系动态添加行为,而无须改变继承体系.但是GOF访问者模式的缺 ...
- Visitor模式,Decorator模式,Extension Object模式
Modem结构 Visitor模式 对于被访问(Modem)层次结构中的每一个派生类,访问者(Visitor)层次中都有一个对应的方法. 从派生类到方法的90度旋转. 新增类似的Windows配置函数 ...
- c++ 访问者模式(visitor pattern)
概述: 我们去银行柜台办业务,一般情况下会开几个个人业务柜台的,你去其中任何一个柜台办理都是可以的.我们的访问者模式可以很好付诸在这个场景中:对于银行柜 台来说,他们是不用变化的,就是说今天和明天提供 ...
随机推荐
- ubuntu 命令学习大全
http://wiki.ubuntu.org.cn/UbuntuSkills#.E6.98.BE.E7.A4.BA.E5.BD.93.E5.89.8D.E7.A1.AC.E4.BB.B6.E4.BF. ...
- struts2标签获取parameter,request,session,application中的值
http://localhost:8080/demo/index.jsp?flag=kkkk <s:property value="#parameters.flag" /&g ...
- C++学习48 对ASCII文件的读写操作
如果文件的每一个字节中均以ASCII代码形式存放数据,即一个字节存放一个字符,这个文件就是ASCII文件(或称字符文件).程序可以从ASCII文件中读入若干个字符,也可以向它输出一些字符. 对ASCI ...
- java异常处理机制Exception
Exception是一个整体的异常,子类NullPointerException.StringIndexOutOfBoundsException 异常处理语句 try{ 可能发生异常的代码片段 }ca ...
- 山东省第四届ACM省赛
排名:http://acm.sdut.edu.cn/sd2012/2013.htm 解题报告:http://www.tuicool.com/articles/FnEZJb A.Rescue The P ...
- c# list排序的三种实现方式
用了一段时间的gridview,对gridview实现的排序功能比较好奇,而且利用C#自带的排序方法只能对某一个字段进行排序,今天demo了一下,总结了三种对list排序的方法,并实现动态传递字段名对 ...
- DNS拦截的处理
在用webSocket来实现长连接时,我们的链接对象使用了域名.但是再某些省份的网络下,发生了DNS拦截.踹改.导致使用某个域名链接,发生连接不上的现象.[解决方案] 在多次尝试原有域名不能使用的情况 ...
- 【转】nginx服务器安装及配置文件详解
原文:http://seanlook.com/2015/05/17/nginx-install-and-config/ nginx服务器安装及配置文件详解 nginx在工作中已经有好几个环境在使用了, ...
- Qt下QWizard改变next,back等默认按钮的大小及背景
默认的按钮又小又丑,想改大点漂亮点. 百度没百出来,最后用google 在这里: http://stackoverflow.com/questions/16425575/change-qwizard- ...
- Windows操作 - Photoshop为图片添加透明立体水印
原文地址:http://design.yesky.com/photoshop/252/2427752.shtml 本文我们介绍用Photoshop为图片添加透明立体水印的方法和技巧. 原图: 打开原图 ...