#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. tony_linux下网站压力测试工具webbench

    webbench最多可以模拟3万个并发连接去测试网站的负载能力,个人感觉要比Apache自带的ab压力测试工具好,安装使用也特别方便. 1.适用系统:Linux 2.编译安装:wget http:// ...

  2. .NET 验证码/验证图片

    /// <summary> /// 验证码类 /// </summary> public class Rand { #region 生成随机数字 /// <summary ...

  3. 为何你的php代码没有写结束标签

    PHP闭合标签"?>"在PHP中对PHP的分析器是可选的.但是,如果使用闭合标签,任何由开发者,用户, 或者FTP应用程序插入闭合标签后面的空格都有可能会引起多余的输出.ph ...

  4. 如何判断VS.NET设计时?

    方法一: if (System.Diagnostics.Process.GetCurrentProcess().ProcessName == "devenv") //...Then ...

  5. Rehat 5.8下oracle11g安装

    Oracle 11g Redhat 5安装  一.检查硬件是否满足要求 检查一内存 On Linux x86: At least 1 GB of RAM 内存至少1G To determine the ...

  6. 题目1005:Graduate Admission

    题目1005:Graduate Admission 时间限制:1 秒 内存限制:32 兆 特殊判题:否 题目描述: It is said that in 2011, there are about 1 ...

  7. Android中常用的5大布局详述

    Android的界面是有布局和组件协同完成的,布局好比是建筑里的框架,而组件则相当于建筑里的砖瓦.组件按照布局的要求依次排列,就组成了用户所看见的界面. 所有的布局方式都可以归类为ViewGroup的 ...

  8. 【翻译】什么是 eCPM & RPM 与其计算公式

    [原文链接] What is eCPM & RPM. How to Calculate eCPM & RPM using Formula eCPM代表着每千次网页爆光转换率(或者是每千 ...

  9. Flex 4 不同主题下容器子元素的管理方法

    Flex 下,容器主要分两类:Spark容器, Halo容器. Spark容器 Halo容器 说明 numElements numChildern 容器的子元素数量. addElement( ) ad ...

  10. C# Exception 对象的属性

    关于 C# 中 Exception 对象的属性,在程序出现异常时,需要记录异常的信息,并把信息保存起来或输出,以方便准确定位异常的根源,以下是 Exception  对象的属性 名称          ...