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)
概述: 我们去银行柜台办业务,一般情况下会开几个个人业务柜台的,你去其中任何一个柜台办理都是可以的.我们的访问者模式可以很好付诸在这个场景中:对于银行柜 台来说,他们是不用变化的,就是说今天和明天提供 ...
随机推荐
- java静态代理
WorkIF.java package com.wzh.test; public interface WorkIf { void doWork(String name);} work.java pac ...
- .NET 验证码/验证图片
/// <summary> /// 验证码类 /// </summary> public class Rand { #region 生成随机数字 /// <summary ...
- 《一课经济学》书摘笔记III
基本谬论:世界上可做的工作是有限的.用更有效率的方式去做事,只会消减工作机会.这个信条换句话说就是,采用低效率的方式去做一件事,反而可以创造工作机会. 只要还有人的需要或愿望还没有获得满足,能做的事就 ...
- Unity中对象池的使用
unity中用到大量重复的物体,例如发射的子弹,可以引入对象池来管理,优化内存. 对象池使用的基本思路是: 将用过的对象保存起来,等下一次需要这种对象的时候,再拿出来重复使用.恰当地使用对象池,可以在 ...
- Network of Schools(强连通分量缩点(邻接表&矩阵))
Description A number of schools are connected to a computer network. Agreements have been developed ...
- Spring Data Jpa 规范接口表
Keyword Sample JPQL snippet And findByLastnameAndFirstname … where x.lastname = ?1 and x.firstname ...
- ListView之setEmptyView的问题
使用listView或者gridView时,当列表为空时,有时需要显示一个特殊的empty view来提示用户,一般情况下,如果你是继承ListActivity,只要 <ListView and ...
- (转)C#调用非托管Win 32 DLL
转载学习收藏,原文地址http://www.cnblogs.com/mywebname/articles/2291876.html 背景 在项目过程中,有时候你需要调用非C#编写的DLL文件,尤其在使 ...
- BPMN
1.私有业务流程: 特定行业规则制度比如惠普生产线流程-针对业务人员 2.公有业务流程: 技术实现-针对流程开发人员 http://www.blogjava.net/RongHao/archive/2 ...
- php文件删除unlink()详解
请记住从PHP文件创建的教训,我们创建了一个文件,名为testFile.txt . $myFile = "testFile.txt"; $fh = fopen($myFile, ' ...