听峰问雨 听峰问雨
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)

zfprotectors

默默学习er
首页
导航站
  • 编程语言

    • Python
  • 数据结构与算法
  • 设计模式
  • UVA
  • LeetCode
  • 《Go语言实战》
  • 《Go Web编程》
  • 《算法精粹 经典计算机科学问题的Python实现》
  • 学习
  • 博客搭建
  • 本站

    • 分类
    • 标签
    • 归档
  • 我的

    • 收藏
    • 关于
GitHub (opens new window)
  • Python

  • Go

  • 数据结构与算法

  • 设计模式

    • 【设计模式】1 引言
    • 创建型

    • 结构型

    • 行为型

      • 【设计模式】行为型模式-Chain of Responsibility模式
      • 【设计模式】行为型模式-Command模式
      • 【设计模式】行为型模式-Interpreter模式
      • 【设计模式】行为型模式-Iterator模式
      • 【设计模式】行为型模式-Mediator模式
      • 【设计模式】行为型模式-Memento模式
      • 【设计模式】行为型模式-Observer模式
      • 【设计模式】行为型模式-State模式
      • 【设计模式】行为型模式-Strategy模式
      • 【设计模式】行为型模式-Template模式
      • 【设计模式】行为型模式-Visitor模式
  • 程序设计层
  • 设计模式
  • 行为型
zfprotectors
2022-05-18

【设计模式】行为型模式-Visitor模式

# 功能

主要将数据结构与数据操作分离

# 解决

  • 主要解决:稳定的数据结构和易变的操作耦合问题
  • 何时使用:需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免让这些操作"污染"这些对象的类,使用访问者模式将这些封装到类中
  • 如何解决:在被访问的类里面加一个对外提供接待访问者的接口
  • 关键代码:在数据基础类里面有一个方法接受访问者,将自身引用传入访问者

# 优缺点

  • 优点:
    • 符合单一职责原则
    • 优秀的扩展性
    • 灵活性
  • 缺点:
    • 具体元素对访问者公布细节,违反了迪米特原则
    • 具体元素变更比较困难
    • 违反了依赖倒置原则,依赖了具体类,没有依赖抽象

# 应用场景

  • 应用实例:您在朋友家做客,您是访问者,朋友接受您的访问,您通过朋友的描述,然后对朋友的描述做出一个判断,这就是访问者模式
  • 使用场景:
    • 对象结构中对象对应的类很少改变,但经常需要在此对象结构上定义新的操作
    • 需要对一个对象结构中的对象进行很多不同的并且不相关的操作,而需要避免让这些操作"污染"这些对象的类,也不希望在增加新操作时修改这些类
  • 注意事项:访问者可以对功能进行统一,可以做报表、UI、拦截器与过滤器

# 简单示例代码部分

Visitor.h

#ifndef _VISITOR_H_ 
#define _VISITOR_H_ 
#include <iostream>
using namespace std;
class ConcreteElementA; 
class ConcreteElementB; 
class Element;
class Visitor{
public:
	virtual ~Visitor(){}
	virtual void VisitConcreteElementA(Element* elm) = 0;
	virtual void VisitConcreteElementB(Element* elm) = 0; 
protected:
	Visitor(){}
};

class ConcreteVisitorA:public Visitor {
public:
	ConcreteVisitorA(){}
	virtual ~ConcreteVisitorA(){}
	virtual void VisitConcreteElementA(Element* elm){
		cout<<"i will visit ConcreteElementA..."<<endl;
	}
	virtual void VisitConcreteElementB(Element* elm){
		cout<<"i will visit ConcreteElementB..."<<endl;
	}
};

class ConcreteVisitorB:public Visitor {
public:
	ConcreteVisitorB(){}
	virtual ~ConcreteVisitorB(){}
	virtual void VisitConcreteElementA(Element* elm){
		cout<<"i will visit ConcreteElementA..."<<endl;
	}
	virtual void VisitConcreteElementB(Element* elm){
		cout<<"i will visit ConcreteElementB..."<<endl;
	}
};

class Element{
public:
	virtual ~Element(){}
	virtual void Accept(Visitor* vis) = 0; 
protected:
	Element(){}
};

class ConcreteElementA:public Element {
public:
	ConcreteElementA(){}
	~ConcreteElementA(){} 
	void Accept(Visitor* vis){
		vis->VisitConcreteElementA(this);
		cout<<"visiting ConcreteElementA..."<<endl;
	}
};
class ConcreteElementB:public Element {
public: 
	ConcreteElementB(){}
	~ConcreteElementB(){}
	void Accept(Visitor* vis){
		cout<<"visiting ConcreteElementB..."<<endl;
		vis->VisitConcreteElementB(this);
	}
};
#endif
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67

main.cpp

#include "Visitor.h"
#include <iostream>
using namespace std;
int main() {
	Visitor* vis = new ConcreteVisitorA();
	Element* elm = new ConcreteElementA();
	elm->Accept(vis);
	return 0;
}
1
2
3
4
5
6
7
8
9
编辑 (opens new window)
#设计模式
上次更新: 2022/05/18, 15:47:37
【设计模式】行为型模式-Template模式

← 【设计模式】行为型模式-Template模式

最近更新
01
LeetCode88 - 合并两个有序数组
06-22
02
LeetCode1 - 两数之和
06-22
03
LeetCode1603 - 设计停车系统
06-21
更多文章>
Theme by Vdoing | Copyright © 2021-2022 zfprotectors | 闽ICP备2021014222号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式