长生栈 长生栈
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

Living Team

编程技术分享
首页
  • 编程语言

    • C语言
    • C++
    • Java
    • Python
  • 数据结构和算法

    • 全排列算法实现
    • 动态规划算法
  • CMake
  • gitlab 安装和配置
  • docker快速搭建wordpress
  • electron+react开发和部署
  • Electron-创建你的应用程序
  • ImgUI编译环境
  • 搭建图集网站
  • 使用PlantUml画时序图
  • 友情链接
关于
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • 创建型模式

  • 结构型模式

  • 行为型模式

    • 责任链模式(Chain of Responsibility Pattern)
    • 命令模式(Command Pattern)
    • 解释器模式(Interpreter Pattern)
    • 迭代器模式(Iterator Pattern)
    • 中介者模式(Mediator Pattern)
    • 备忘录模式(Memento Pattern)
    • 观察者模式(Observer Pattern)
    • 状态模式(State Pattern)
    • 策略模式(Strategy Pattern)
    • 模板方法模式(Template Method Pattern)
    • 访问者模式(Visitor Pattern)
      • 概念
      • 思想
      • 角色
      • 优点
      • 缺点
      • 类图
      • 时序图
      • 示例代码
  • 设计模式
  • 行为型模式
DC Wang
2023-05-28
目录

访问者模式(Visitor Pattern)

# 访问者模式(Visitor Pattern)

# 概念

访问者模式是一种行为型设计模式,它允许在不改变被访问对象的类的前提下,定义新的操作。

# 思想

该模式通过将新的操作封装在访问者对象中,使得对被访问对象的操作可以独立于对象的结构。被访问对象提供一个接受访问者的方法,访问者对象则根据需要访问被访问对象的不同部分。

# 角色

  • 访问者(Visitor):定义了对每个元素对象的访问操作接口,通过多态方式实现对不同元素的不同访问操作。
  • 具体访问者(ConcreteVisitor):实现了访问者接口,具体实现对元素的访问操作。
  • 元素(Element):定义了接受访问者访问的接口,通常包含一个接受访问者的方法。
  • 具体元素(ConcreteElement):实现了元素接口,提供了具体的接受访问者方法。
  • 结构对象(ObjectStructure):包含元素的集合,并提供遍历集合的方法。

# 优点

  • 将相关的操作封装在一个访问者中,有利于代码的组织和维护。
  • 增加新的操作时,只需要实现新的访问者,无需修改现有的元素类。
  • 扩展性好,可以通过添加新的访问者来扩展操作。

# 缺点

  • 增加新的元素类时,需要修改所有的访问者类,违反了开闭原则。
  • 增加新的访问者类时,需要修改所有的元素类,违反了开闭原则。

# 类图

@startuml

class Client {
  +main()
}

class Visitor {
  +visitElementA(elementA: ElementA)
  +visitElementB(elementB: ElementB)
}

class ConcreteVisitor1 {
  +visitElementA(elementA: ElementA)
  +visitElementB(elementB: ElementB)
}

class ConcreteVisitor2 {
  +visitElementA(elementA: ElementA)
  +visitElementB(elementB: ElementB)
}

class Element {
  +accept(visitor: Visitor)
}

class ElementA {
  +accept(visitor: Visitor)
  +operationA()
}

class ElementB {
  +accept(visitor: Visitor)
  +operationB()
}

class ObjectStructure {
  +addElement(element: Element)
  +removeElement(element: Element)
  +accept(visitor: Visitor)
}

Client --> Visitor
ConcreteVisitor1 --> Visitor
ConcreteVisitor2 --> Visitor
ElementA --> Element
ElementB --> Element
ElementA --> ObjectStructure
ElementB --> ObjectStructure
ObjectStructure --> Visitor

@enduml
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

# 时序图

@startuml

client -> visitor: visitElementA(elementA)
activate visitor
visitor -> elementA: accept(visitor)
activate elementA
elementA --> visitor: operationA()
deactivate elementA
visitor --> client: (perform visitor-specific operations)

@enduml
1
2
3
4
5
6
7
8
9
10
11

# 示例代码

#include <iostream>

class Visitor;

class Element {
public:
    virtual void accept(Visitor& visitor) = 0;
};

class ElementA : public Element {
public:
    void accept(Visitor& visitor) override;
    void operationA() {
        std::cout << "ElementA operationA" << std::endl;
    }
};

class ElementB : public Element {
public:
    void accept(Visitor& visitor) override;
    void operationB() {
        std::cout << "ElementB operationB" << std::endl;
    }
};

class Visitor {
public:
    virtual void visitElementA(ElementA& elementA) = 0;
    virtual void visitElementB(ElementB& elementB) = 0;
};

class ConcreteVisitor1 : public Visitor {
public:
    void visitElementA(ElementA& elementA) override {
        std::cout << "ConcreteVisitor1 visiting ElementA" << std::endl;
        elementA.operationA();
    }

    void visitElementB(ElementB& elementB) override {
        std::cout << "ConcreteVisitor1 visiting ElementB" << std::endl;
        elementB.operationB();
    }
};

class ConcreteVisitor2 : public Visitor {
public:
    void visitElementA(ElementA& elementA) override {
        std::cout << "ConcreteVisitor2 visiting ElementA" << std::endl;
        elementA.operationA();
    }

    void visitElementB(ElementB& elementB) override {
        std::cout << "ConcreteVisitor2 visiting ElementB" << std::endl;
        elementB.operationB();
    }
};

class ObjectStructure {
private:
    std::vector<Element*> elements;

public:
    void addElement(Element* element) {
        elements.push_back(element);
    }

    void removeElement(Element* element) {
        // Remove element from the collection
    }

    void accept(Visitor& visitor) {
        for (Element* element : elements) {
            element->accept(visitor);
        }
    }
};

void ElementA::accept(Visitor& visitor) {
    visitor.visitElementA(*this);
}

void ElementB::accept(Visitor& visitor) {
    visitor.visitElementB(*this);
}

int main() {
    ObjectStructure objectStructure;
    objectStructure.addElement(new ElementA());
    objectStructure.addElement(new ElementB());

    ConcreteVisitor1 visitor1;
    objectStructure.accept(visitor1);

    ConcreteVisitor2 visitor2;
    objectStructure.accept(visitor2);

    return 0;
}
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98

示例代码中定义了两个具体元素类 ElementA 和 ElementB,它们都继承自抽象元素类 Element,并实现了 accept 方法。有两个具体访问者类 ConcreteVisitor1 和 ConcreteVisitor2,它们都继承自抽象访问者类 Visitor,并实现了 visitElementA 和 visitElementB 方法。ObjectStructure 类用于管理元素对象的集合,并提供了接受访问者的方法。

在示例代码的 main 函数中,创建了一个 ObjectStructure 对象,并添加了一个 ElementA 对象和一个 ElementB 对象。然后分别创建了 ConcreteVisitor1 和 ConcreteVisitor2 对象,并将它们作为参数传递给 ObjectStructure 对象的 accept 方法。accept 方法会遍历元素。

示例代码的输出如下:

ConcreteVisitor1 visiting ElementA
ElementA operationA
ConcreteVisitor1 visiting ElementB
ElementB operationB
ConcreteVisitor2 visiting ElementA
ElementA operationA
ConcreteVisitor2 visiting ElementB
ElementB operationB
1
2
3
4
5
6
7
8

首先,ConcreteVisitor1 访问 ElementA,输出了相应的访问信息,并调用了 ElementA 的 operationA 方法。然后,ConcreteVisitor1 访问 ElementB,同样输出访问信息,并调用了 ElementB 的 operationB 方法。

接下来,ConcreteVisitor2 访问 ElementA 和 ElementB,输出相应的访问信息,并分别调用了它们各自的操作方法。

整体上,每个访问者都按照自己的逻辑访问了不同的元素,并执行了相应的操作。

编辑 (opens new window)
#设计模式#行为型模式
上次更新: 2023/06/10, 08:05:09
模板方法模式(Template Method Pattern)

← 模板方法模式(Template Method Pattern)

最近更新
01
ESP32-网络摄像头方案
06-14
02
ESP32-PWM驱动SG90舵机
06-14
03
ESP32-实时操作系统freertos
06-14
更多文章>
Theme by Vdoing | Copyright © 2019-2025 DC Wang All right reserved | 辽公网安备 21021102001125号 | 吉ICP备20001966号-2
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式