设计模式(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)

相关推荐
博风1 小时前
8、设计模式应用举例:适配器模式:Iterator接口与Enumeration接口
设计模式·适配器模式
高晶2 小时前
ARM 嵌入式 Linux 系统 U-Boot 开发与移植实战M
设计模式·程序员
geovindu7 小时前
rust: Chain of Responsibility Pattern
开发语言·后端·设计模式·rust·责任链模式·行为模式
拆房老料8 小时前
【第四部分:Agentic 智能体设计模式】15.为什么 Agent 也需要设计模式
人工智能·设计模式·ai·ai-native
geovindu9 小时前
rust: Chain of Responsibility Pattern (续)
开发语言·后端·设计模式·rust·责任链模式·行为模式
Lost of 程序猿10 小时前
责任链模式实战:审批流里“层层把关“的艺术
后端·设计模式·asp.net·责任链模式
希洛兔1 天前
状态模式详解:让对象随内部状态自动改变行为
java·设计模式·状态模式
博风1 天前
7、设计模式应用举例:策略模式:加密、解密文件
设计模式·策略模式
4SAPI1 天前
Provider Adapter设计模式:如何让AI应用支持多个模型
网络·人工智能·设计模式
geovindu1 天前
rust: pool Pattern
开发语言·后端·设计模式·rust·对象池简写模式