訪问者设计模式是已经有了一组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. uva1627 Team them up!

    注意这题要求互相认识不认识的人之间连一条线一个人在组1,那么不认识(互相认识)的人就在组0:同时这些人不认识的人就在组1.每个联通分量都可以独立推导,遇到矛盾则无解一个联通分量有一个核心,其他的点是分 ...

  2. 目录下 shift 右键菜单 打开cmd 或者在 地址栏输入cmd 回车进入cmd

    目录下 shift 右键菜单 打开cmd 或者在 地址栏输入cmd 回车进入cmd

  3. dockerfile 的最佳实践

    Dockerfile 编写nginx容器 [root@mast nginx]# cat Dockerfile FROM centos MAINTAINER zhaoruidong RUN yum -y ...

  4. 利用CWinThread实现跨线程父子MFC窗口

    利用CWinThread实现跨线程父子MFC窗口 MFC对象只能由创建该对象的线程访问,而不能由其他线程访问. 不遵守该准则将导致断言(assertion)或者无法预知的程序行为等运行期错误. 在多线 ...

  5. 【搜索】P1041 传染病控制

    题目链接:P1041 传染病控制 题解: 这个题目是看别人的博客做出来的,其实挺不错的一个题目,考察的东西挺多的, 一个dfs可以处理5个东西: 1.找出父亲 2.找出深度 3.每一层的节点,存进Ve ...

  6. Yii1 用commandBuilder方法往数据表中插入多条记录

    $builder = Yii::app()->db->schema->commandBuilder; // 创建builder对象 $command = $builder->c ...

  7. metasploitable2更改root密码

    metasploitable2这个系统众所周知,一个用户名和密码是msfadmin.但是这个账号权限不全,我们想要改root密码来登陆为所欲为.也没试过破解,咱们索性就改了吧. 就简单几行代码..   ...

  8. InsecureRequestWarning: Unverified HTTPS request is being made.解决方法

    在前面添加: import requests from requests.packages.urllib3.exceptions import InsecureRequestWarning reque ...

  9. SpringMVC中的几种事务管理器

    转载https://blog.csdn.net/qq_26222859/article/details/52032853 1JDBC及iBATIS.MyBatis框架事务管理器 <bean id ...

  10. LeetCode (17)Letter Combinations of a Phone Number

    题目 Given a digit string, return all possible letter combinations that the number could represent. A ...