-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisitor.cpp
More file actions
78 lines (63 loc) · 1.56 KB
/
visitor.cpp
File metadata and controls
78 lines (63 loc) · 1.56 KB
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
#include <iostream>
using namespace std;
class Element1;
class Element2;
// 访问者接口
class IVisitor {
public:
virtual void visit_element1(Element1& ele1) = 0;
virtual void visit_element2(Element2& ele2) = 0;
virtual ~IVisitor() = default;
};
// 元素接口
class IElement {
public:
virtual void accept(IVisitor& vis) = 0;
virtual ~IElement() = default;
};
// 元素 1
class Element1 : public IElement {
public:
void accept(IVisitor& vis) override {
vis.visit_element1(*this);
}
};
// 元素 2
class Element2 : public IElement {
public:
void accept(IVisitor& vis) override {
vis.visit_element2(*this);
}
};
// 为 IElement 添加 func1 接口
class Visitor1 : public IVisitor {
void visit_element1(Element1& ele1) override {
cout << "Element1 func1" << endl;
}
void visit_element2(Element2& ele2) override {
cout << "Element2 func1" << endl;
}
};
// 为 IElement 添加 func2 接口
class Visitor2 : public IVisitor {
void visit_element1(Element1& ele1) override {
cout << "Element1 func2" << endl;
}
void visit_element2(Element2& ele2) override {
cout << "Element2 func2" << endl;
}
};
int main() {
Visitor1 vis1;
Visitor2 vis2;
// IElement 看着没有 func1、func2 接口
// 但通过访问者模式为其 “添加” 了这两个接口
IElement* ele1 = new Element1();
ele1->accept(vis1);
ele1->accept(vis2);
cout << endl;
IElement* ele2 = new Element2();
ele2->accept(vis1);
ele2->accept(vis2);
return 0;
}