设计模式(4)--对象行为(11)--访问者

1. 意图

表示一个作用于某对象结构中的各元素的操作。

使你可以在不改变各元素的类的前提下定义于作用于这些元素的新操作。

2. 五种角色

抽象访问者(Visitor)、具体访问者(Concrete Visitor)、抽象元素(Element)、

具体元素(Concrete Element)、对象结构(ObjectStructure)

3. 优点

3.1 易于增加新的操作

3.2 集中相关的操作,而分离无关的操作

3.3 可以访问不具有相同父类的对象

4. 缺点

4.1 增加新的具体元素很困难

4.2 可能累积状态

4.3 可能破坏元素的封装性

5. 相关模式

5.1 访问者可以对一个由Composite模式定义的对象结构进行操作。

5.2 访问者可以用于解释器。

6. 代码示意
cpp 复制代码
#pragma once
#include <vector>
#include <string>
#include <iostream>
using namespace std;

class ElementA;
class ElementB;

class Visitor
{
public:
	virtual void VisitElementA(ElementA* pElementA) = 0;
	virtual void VisitElementB(ElementB* pElementB) = 0;
};

class ConcreteVisitor1 : public Visitor
{
public:
	virtual void VisitElementA(ElementA* pElementA);
	virtual void VisitElementB(ElementB* pElementB);
};
class ConcreteVisitor2 : public Visitor
{
public:
	virtual void VisitElementA(ElementA* pElementA);
	virtual void VisitElementB(ElementB* pElementB);
};

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

class ElementA : public Element
{
public:
	virtual void Accept(Visitor* pVisitor) {
		pVisitor->VisitElementA(this);
	}
	void OperationA(const string& name) {
		cout << "ElementA 来自 " << name << " 的访问" << endl;
	}
};

class ElementB : public Element
{
public:
	virtual void Accept(Visitor* pVisitor) {
		pVisitor->VisitElementB(this);
	}
	void OperationB(const string& name) {
		cout << "ElementB 来自 " << name << " 的访问" << endl;
	}
};


class ObjectStructure
{
	vector<Element*> m_elememts;
public:
	ObjectStructure() {
		m_elememts.emplace_back(new ElementA());
		m_elememts.emplace_back(new ElementB());
	}
	~ObjectStructure() {
		auto it = m_elememts.begin();
		while (it != m_elememts.end()) {
			delete* it;
			++it;
		}
	}
	void Visit(Visitor* pVisitor) {
		auto it = m_elememts.begin();
		while (it != m_elememts.end()) {
			(*it)->Accept(pVisitor);
			++it;
		}
	}
};

Visitor.cpp:

cpp 复制代码
#include "Visitor.h"

void ConcreteVisitor1::VisitElementA(ElementA* pElementA) {
	pElementA->OperationA("Visitor1");
}

void ConcreteVisitor1::VisitElementB(ElementB* pElementB) {
	pElementB->OperationB("Visitor1");
}

void ConcreteVisitor2::VisitElementA(ElementA* pElementA) {
	pElementA->OperationA("Visitor2");
}

void ConcreteVisitor2::VisitElementB(ElementB* pElementB) {
	pElementB->OperationB("Visitor2");
}
cpp 复制代码
#include "Visitor.h"
int main() {
	ObjectStructure* pObjStructure = new ObjectStructure();
	Visitor* pVisitor = new ConcreteVisitor1();
	pObjStructure->Visit(pVisitor);
	delete pVisitor;

	pVisitor = new ConcreteVisitor2();
	pObjStructure->Visit(pVisitor);
	delete pVisitor;

	delete pObjStructure;
	return 0;
}

运行结果:

6.1 ObjectStructure提供访问所有元素的接口

6.2 增加新的ConcreteVisitor3就可以定义一个新的操作(3.1)

6.3 抽象Visitor里的接口集中了相关的操作(3.2)

6.4 增加新的ElementC,所有Visitor类和ObjectStructure都要修改(4.1)

相关推荐
AI酒旅实战开发17 小时前
旅行 AI Agent 的 7 种设计模式:从工具调用到交易闭环
人工智能·设计模式
geovindu2 天前
rust: Functional Options Pattern
开发语言·后端·设计模式·rust·函数式选项模式·惯用法型模式
geovindu4 天前
rust: Timing Functions Pattern
开发语言·后端·设计模式·rust·计时函数模式·性能分析型模式
Mh8 天前
我是如何设计品牌站组件库的?
前端·css·设计模式
是2的10次方啊9 天前
设计模式与生活·总论:AI 能替你砌墙,替不了你打地基
设计模式
dahaihua3339 天前
用 SysML 表达航电系统架构(附建模示例)
设计模式·重构·系统架构·集成测试·需求分析·学习方法
wy_hhxx10 天前
《Agent设计模式 图解可复用智能体架构》上篇内容整理
设计模式·架构·ai agent
AAA修煤气灶刘哥10 天前
DSH Super-agent 双层架构:如何兼顾后台执行与持续响应
人工智能·后端·设计模式
用户6242931966211 天前
告别 Electron 臃肿与 Wireshark 老化界面:用 Rust + Tauri 2.0 构建多总线网络与现场总线深度分析仪 Omni-Probe
设计模式
是2的10次方啊11 天前
设计模式与生活㉒:暖气热不热,得一间一间摸过去
设计模式