#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. windows 修改hosts

    2222.111.1.1 ip 格式要正确,否则硬解会失败. 220.11.1.1

  2. Dubbo 服务暴露注册流程

    Dubbo的应用会在启动时完成服务注册或订阅(不论是生产者,还是消费者)如下图所示. 图中小方块Protocol, Cluster, Proxy, Service, Container, Regist ...

  3. asp.net 安全性相关参考

    <system.web> <depolyment retail="true" /> </system.web>   AntiXSS http:/ ...

  4. (medium)LeetCode 227.Basic Calculator II

    Implement a basic calculator to evaluate a simple expression string. The expression string contains ...

  5. Caught exception while loading file struts-default.xml 错误

    严重: Exception starting filter struts2 Caught exception while loading file struts-default.xml - [unkn ...

  6. HDU 4771 Stealing Harry Potter's Precious

    Stealing Harry Potter's Precious Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 ...

  7. 这些优化 Drupal 网站速度的超简单办法,你忽略了多少?

    “怎么样能让我的 Drupal 网站更快一些?”是我们最常遇到的一个问题.站点速度确实非常重要,因为它会影响你的 SEO排名效果.访客是否停留以及你自己管理网站所需要的时间. 今天我们就来看看那些通过 ...

  8. 对.Net系统架构改造的一点经验和教训

    如果你对项目管理.系统架构有兴趣,请加微信订阅号"softjg",加入这个PM.架构师的大家庭 在互联网行业,基于Unix/Linux的网站系统架构毫无疑问是当今主流的架构解决方案 ...

  9. ApplePay

    ApplePay要在项目有里配置,,配置好项目之后,就剩下编码了,做ApplePay首先要检查设备是否支持ApplePay,支持 ApplePay的设备在 iPhone6及以后,  PKPayment ...

  10. 洛谷P1204 [USACO1.2]挤牛奶Milking Cows

    P1204 [USACO1.2]挤牛奶Milking Cows 474通过 1.4K提交 题目提供者该用户不存在 标签USACO 难度普及- 提交  讨论  题解 最新讨论 请各位帮忙看下程序 错误 ...