#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的更多相关文章

  1. 完成C++不能做到的事 - Visitor模式

    拿着刚磨好的热咖啡,我坐在了显示器前.“美好的一天又开始了”,我想. 昨晚做完了一个非常困难的任务并送给美国同事Review,因此今天只需要根据他们提出的意见适当修改代码并提交,一周的任务就完成了.剩 ...

  2. 十一个行为模式之访问者模式(Visitor Pattern)

    定义: 提供一个作用于某对象结构(通常是一个对象集合)的操作的接口,使得在添加新的操作或者在添加新的元素时,不需要修改原有系统,就可以对各个对象进行操作. 结构图: Visitor:抽象访问者类,对元 ...

  3. 设计模式之observer and visitor

    很长时间一直对observer(观察者)与visitor(访问者)有些分不清晰. 今天有时间进行一下梳理: 1.observer模式 这基本就是一个通知模式,当被观察者发生改变时,通知所有监听此变化的 ...

  4. 访问者模式(Visitor Pattern)

    定义:封装某些作用于某种数据结构中各元素的操作,它可以在不改变数据结构的前提下定义作用于这些元素的新的操作. Visitor 抽象访问者角色:为该对象结构中具体元素角色声明一个访问操作接口.该操作接口 ...

  5. 设计模式之美:Visitor(访问者)

    索引 意图 结构 参与者 适用性 效果 相关模式 实现 实现方式(一):Visitor 模式结构样式代码. 实现方式(二):使用 Visitor 模式解构设计. 实现方式(三):使用 Acyclic ...

  6. 访问者(Visitor)模式

    http://www.cnblogs.com/zhenyulu/articles/79719.html 一. 访问者(Visitor)模式 访问者模式的目的是封装一些施加于某种数据结构元素之上的操作. ...

  7. 深入浅出设计模式——访问者模式(Visitor Pattern)

    模式动机 对于系统中的某些对象,它们存储在同一个集合中,且具有不同的类型,而且对于该集合中的对象,可以接受一类称为访问者的对象来访问,而且不同的访问者其访问方式有所不同,访问者模式为解决这类问题而诞生 ...

  8. 无环的visitor模式

    无环的访问者模式,是来改进原有访问者模式的不足之处的,是Robert C. Martin首次提出的.我们知道访问者模式的优点是为被访问继承体系动态添加行为,而无须改变继承体系.但是GOF访问者模式的缺 ...

  9. Visitor模式,Decorator模式,Extension Object模式

    Modem结构 Visitor模式 对于被访问(Modem)层次结构中的每一个派生类,访问者(Visitor)层次中都有一个对应的方法. 从派生类到方法的90度旋转. 新增类似的Windows配置函数 ...

  10. c++ 访问者模式(visitor pattern)

    概述: 我们去银行柜台办业务,一般情况下会开几个个人业务柜台的,你去其中任何一个柜台办理都是可以的.我们的访问者模式可以很好付诸在这个场景中:对于银行柜 台来说,他们是不用变化的,就是说今天和明天提供 ...

随机推荐

  1. Ubuntu 13.10 安装 ia32-lib

    Ubuntu 13.10下面不参直接安装ia32-libs,直接安装的时候会提示下面的信息: output$ sudo apt-get install ia32-libs Reading packag ...

  2. 一步步教你如何把电脑设置U盘启动(图解教程)

    一.我们先来说说如何进入 BIOS设置程序: 在开机时按下特定的热键可以进入BIOS设置程序,不同类型的机器进入BIOS设置程序的按键不同,有的在屏幕上给出提示,有的不给出提示,几种常见的BIOS设置 ...

  3. codeblocks安装后无法编译

    codeblocks安装后无法编译: 解决办法: 1.下载自带编译器的codeblock安装包. 2.安装完后.修改配置 (1)打开软件,选择setting->Compiler (2)在编译器设 ...

  4. 编写一个go gRPC的服务

    前置条件: 获取 gRPC-go 源码 $ go get google.golang.org/grpc 简单例子的源码位置: $ cd $GOPATH/src/google.golang.org/gr ...

  5. 组播(Multicast)传输

    组播(Multicast)传输: 在发送者和每一接收者之间实现点对多点网络连接. 如果一台发送者同时给多个的接收者传输相同的数据,也只需复制一份的相同数据包.它提高了数据传送效率.减少了骨干网络出现拥 ...

  6. Net文章汇总帖

    DevExpress:Data Grid ExamplesHow to: Initialize Cells in Newly Created RowsHow to: Set a Cell Value ...

  7. C++primer 练习13.44

    13.44:编写标准库string类的简化版本,命名为String.你的类应该至少有一个默认构造函数和一个接受C 风格字符串指针参数的构造函数.使用allocator为你的String类分配所需内存 ...

  8. JavaScript对象的创建之动态原型方式

    为了让定义的方式更加符合java的需求,就把定义方法的原型代码放置在Person这个构造函数中. function Person(name,age,friends){ //属性在构造函数中定义 thi ...

  9. Grunt 之通配符

    在描述源码路径的时候,经常有一些特殊的奇怪的要求.Grunt 通过内建的 node-glob 和 minimatch 库提供了文件名的扩展机制. 常见的通配符如下: * 匹配除了 / 之外的任意数量的 ...

  10. cocos2d-x 中 TTF 字体文件的位置

    cocos2d-x 中,字体文件需要保存在 fonts 文件夹中,如果字体路径中没有 fonts/ 会自动添加上这个文件夹. 如果字体名称没有 .ttf 后缀,也会自动加上这个后缀. unsigned ...