-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvisitor.cpp
107 lines (91 loc) · 2.37 KB
/
visitor.cpp
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
99
100
101
102
103
104
105
106
107
/*!
* \brief Visitor. Represent an operation to be performed
* on the elements of an object structure. Visitor lets
* you define a new operation without changing the classes
* of the elements on which it operates.
*
* <ObjectStructure> <Element> <Visitor>
* School (create)-> Engineer <==> HR
* | | |
* SoftwareEngineer IncomeVisitor VacationVisitor
*/
#include <iostream>
#include <string>
class Engineer;
// Visitor.
class HR {
public:
virtual void Visit(Engineer *element) {};
};
// Element
class Engineer {
public:
virtual void ShowIncome() = 0;
virtual void ShowVacation() = 0;
// Allow a visitor to access it.
virtual void Accept(HR *visitor) {};
};
// ConcreteElement
class SoftwareEngineer : public Engineer {
public:
SoftwareEngineer(std::string name, int income, int vacation) {
this->name_ = name;
this->income_ = income;
this->vacation_ = vacation;
}
void Accept(HR *visitor) {
visitor->Visit(this);
}
// Show something or do something.
void ShowIncome() {
std::cout << name_ << "'s income: " << income_ << std::endl;
}
void ShowVacation() {
std::cout << name_ << "'s vacation: " << vacation_ << std::endl;
}
public:
std::string name_;
int income_;
int vacation_;
};
// ConcreteVisitor.
class IncomeVisitor : public HR {
public:
void Visit(Engineer *element) {
element->ShowIncome();
}
};
// ConcreteVisitor.
class VacationVisitor : public HR {
public:
void Visit(Engineer *element) {
element->ShowVacation();
}
};
// ObjectStructure. (Element producer).
// It is typically contains multiple containers of different types
// and interfaces, such as List, Set, Map, etc.
class School {
public:
static Engineer *CreateSoftwareEngineer(std::string name,
int income, int vacation) {
Engineer *t = new SoftwareEngineer(name, income, vacation);
return t;
}
};
void main() {
// Create two visitors
HR *v1 = new IncomeVisitor();
HR *v2 = new VacationVisitor();
// Create Engineers by School.
Engineer *Tom = School::CreateSoftwareEngineer("Tom", 25000, 14);
Engineer *Jimmy = School::CreateSoftwareEngineer("Jimmy", 36000, 6);
Tom->Accept(v1);
Tom->Accept(v2);
Jimmy->Accept(v1);
Jimmy->Accept(v2);
delete v1;
delete v2;
delete Tom;
delete Jimmy;
}