訪问者设计模式是已经有了一组Person对象了,然后不同的訪问者訪问这组对象。会有不同效果。

这些訪问者实际上就是一个能够让Person对象组运行的动作行为等。

至于这些Person对象是怎样运行这些訪问者的动作的,那是已经在特定的不同的Person对象中设计好的。

比方我们的訪问者或许是一些动作集合的类,如:

class Action
{
public:
string present;
string gun;
virtual void drinkBeer(Person *p) = 0;
virtual void getAGun(Person *p) = 0;
};

依据这个訪问者基类定义不同的訪问动作:

class ActionOne:public Action
{
public:
ActionOne()
{
present = "Alcohol";
gun = "Laiser";
}
void drinkBeer(Person *p);
void getAGun(Person *p);
}; class ActionTwo:public Action
{
public:
ActionTwo()
{
present = "Beer";
gun = "Machine Gun";
}
void getAGun(Person *p);
void drinkBeer(Person *p);
};

而Person类则运行动作类的不同动作,也是基类动作的一部分,这样呈现出不同Person运行的行为不一样。

class Person
{
public:
string something;
virtual void receivedVisitor(Action *visitor) = 0;
}; class Bill:public Person
{
public:
Bill()
{
something = "food";
}
void receivedVisitor(Action *visitor)
{
puts("\nVisitor to Bill bring :");
puts(visitor->present.c_str());
visitor->drinkBeer(this);
}
}; class Mark:public Person
{
public:
Mark()
{
something = "Weapon";
}
void receivedVisitor(Action *visitor)
{
puts("\nVisitor to Mark bring :");
puts(visitor->gun.c_str());
visitor->getAGun(this);
}
};

使用一个类来存放这些对象:

class House
{
protected:
vector<Person *> vps;
public:
void addPerson(Person *p)
{
vps.push_back(p);
}
void receiver(Action *visitor)
{
for (int i = 0; i < (int)vps.size(); i++)
{
vps[i]->receivedVisitor(visitor);
}
}
~House()
{
for (int i = 0; i < (int)vps.size(); i++)
{
delete vps[i];
}
}
};

最后就在这个类对象中存放须要被訪问的对象,然后使用动作类来訪问这些对象。

总的来说也是思想并不太难的一个设计模式,可是要非常好实现还是不太easy的。

所有代码例如以下:

#include <stdio.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std; class Person; class Action
{
public:
string present;
string gun;
virtual void drinkBeer(Person *p) = 0;
virtual void getAGun(Person *p) = 0;
}; class ActionOne:public Action
{
public:
ActionOne()
{
present = "Alcohol";
gun = "Laiser";
}
void drinkBeer(Person *p);
void getAGun(Person *p);
}; class ActionTwo:public Action
{
public:
ActionTwo()
{
present = "Beer";
gun = "Machine Gun";
}
void getAGun(Person *p);
void drinkBeer(Person *p);
}; class Person
{
public:
string something;
virtual void receivedVisitor(Action *visitor) = 0;
}; class Bill:public Person
{
public:
Bill()
{
something = "food";
}
void receivedVisitor(Action *visitor)
{
puts("\nVisitor to Bill bring :");
puts(visitor->present.c_str());
visitor->drinkBeer(this);
}
}; class Mark:public Person
{
public:
Mark()
{
something = "Weapon";
}
void receivedVisitor(Action *visitor)
{
puts("\nVisitor to Mark bring :");
puts(visitor->gun.c_str());
visitor->getAGun(this);
}
}; void ActionOne::drinkBeer(Person *p)
{
puts("Let's eat something");
puts(p->something.c_str());
} void ActionOne::getAGun(Person *p)
{
puts("Let's gear up");
puts(p->something.c_str());
} void ActionTwo::getAGun(Person *p)
{
puts("Let's gear up");
puts(p->something.c_str());
} void ActionTwo::drinkBeer(Person *p)
{
puts("Let's eat something");
puts(p->something.c_str());
} class House
{
protected:
vector<Person *> vps;
public:
void addPerson(Person *p)
{
vps.push_back(p);
}
void receiver(Action *visitor)
{
for (int i = 0; i < (int)vps.size(); i++)
{
vps[i]->receivedVisitor(visitor);
}
}
~House()
{
for (int i = 0; i < (int)vps.size(); i++)
{
delete vps[i];
}
}
}; int main()
{
House house;
house.addPerson(new Bill);
house.addPerson(new Mark);
ActionTwo gun;
ActionOne drink;
house.receiver(&gun);
house.receiver(&drink);
return 0;
}

运行:

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQva2VuZGVuMjM=/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

Design Pattern Visitor 訪问者设计模式的更多相关文章

  1. Java设计模式(三) Visitor(訪问者)模式及多分派场景应用

    基本概念 Visitor 封装一些作用于数据结构中的各元素的操作,不同的操作能够借助新的visitor实现.减少了操作间的耦合性 訪问者能够将数据结构和对数据的操作解耦,使得添加对数据结构的操作不须要 ...

  2. Design Pattern Explained 读书笔记二——设计模式序言

    设计模式的由来: 20 世纪 90 年代初,一些聪明的开发者偶然接触到 Alexander(Christopher Alexander 的建筑师) 有关模式的工作.他们非常想知道,在建筑学成立的理论, ...

  3. 设计模式之十五:訪问者模式(Visitor Pattern)

    訪问者模式(Visitor Pattern)是GoF提出的23种设计模式中的一种,属于行为模式. 据<大话设计模式>中说算是最复杂也是最难以理解的一种模式了. 定义(源于GoF<De ...

  4. C++ 设计模式 —— 訪问者(Visitor)

    訪问者设计模式的实现借助于两个继承体系, (1)elements:一个是被操作的类(基类及其子类) (2)visitors:一个定义了一系列操作的訪问者(基类及其子类) 訪问者模式是一种行为型设计模式 ...

  5. 设计模式之二十四:訪问者模式(Visitor)

    訪问者模式: 定义了一个作用于一个类的一些操作,訪问者模式同意在不改变类的前提下添加一些操作. Represent an operation to be performed on the elemen ...

  6. Design Pattern: Gof

    Design Pattern: Gof 如果您学习设计模式(Design Pattern),看到Gof这个字,可不要呆呆的没有反应,Gof即Gang of four,也就是四人帮的意思,该设计模式名书 ...

  7. JAVA设计模式之 訪问者模式【Visitor Pattern】

    一.概述 訪问者模式是一种较为复杂的行为型设计模式,它包括訪问者和被訪问元素两个主要组成部分.这些被訪问的元素通常具有不同的类型,且不同的訪问者能够对它们进行不同的訪问操作.在使用訪问者模式时,被訪问 ...

  8. C++设计模式实现--訪问者(Visitor)模式

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/L_Andy/article/details/36896645 一. 訪问者模式 定义:表示一个作用于 ...

  9. 设计模式入门之訪问者模式Visitor

    //訪问者模式定义:表示一个作用于某对象结构中的各个元素的操作,它使你能够在不改变各元素类的前提下定义作用于这些元素的新操作. //从定义上看.这个模式跟装饰模式的定义非常类似(动态地给一个对象加入一 ...

随机推荐

  1. 数据库管理系统X

    大部分DBMS提供数据定义语言DDL(Data Definition Language)和数据操作语言DML(Data Manipulation Language),供用户定义数据库的模式结构与权限约 ...

  2. zipkin 服务追踪

    服务追踪,就是对请求接口的追踪并保存. 在测试的过程中我们会发现,有时候,程序刚刚启动后,刷新几次,并不能看到任何数据,原因就是我们的spring-cloud-sleuth收集信息是有一定的比率的,默 ...

  3. WPF知识点全攻略01- WPF相对WinFrom的优缺点

    对比WPF和WinFrom前,先来了解下GUI现阶段在用的其他一些开发技术: MFC:微软基础类库,以C++的形式封装了Windows API,加上一些实用工具类. QT:奇趣科技开发的跨平台C++图 ...

  4. 主席树-指针实现-找第k小数

    主席树,其实就是N颗线段树 只是他们公用了一部分节点(๑•̀ㅂ•́)و✧ 我大部分的代码是从一位大佬的那里看到的 我这个垃圾程序连Poj2104上的数据都过不了TLE so希望神犇能给我看看, 顺便给 ...

  5. core 中使用 nlog

    引包 代码 public void Configure(IApplicationBuilder app, IHostingEnvironment env,ILoggerFactory logFac) ...

  6. autoHeight # 动态高度添加 用 window.addEventListener('resize', function () {

    动态高度添加 用 window.addEventListener('resize', function () { mounted () { this.init() window.addEventLis ...

  7. PYTHON_DAY_02

    今日内容: 01 列表内置方法 '''''' ''' 列表: 定义: 在[]内,可以存放多个任意类型的值, 并以逗号隔开. 一般用于存放学生的爱好,课堂的周期等等... ''' # 定义一个学生列表, ...

  8. 从多表连接后的select count(*)看待SQL优化

    从多表连接后的select count(*)看待SQL优化 一朋友问我,以下这SQL能直接改写成select count(*) from a吗? SELECT COUNT(*) FROM a LEFT ...

  9. java面试微信交流群-欢迎你的加入

    Java后端技术专注Java相关技术:SSM.Spring全家桶.微服务.MySQL.MyCat.集群.分布式.中间件.Linux.网络.多线程,偶尔讲点运维Jenkins.Nexus.Docker. ...

  10. iptables:ipset批量管理ip

    1.安装 安装: yum -y install ipset \apt-get -y install ipset 2.创建一个ipset ipset create whitelist hash:net ...