-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVisitor.cpp
More file actions
70 lines (58 loc) · 1.23 KB
/
Visitor.cpp
File metadata and controls
70 lines (58 loc) · 1.23 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
/**
* @cite Visitor Pattern states that in order to add new operations to without breaking the OCP and SRP, A visitor structure should be setup beforehand that defines the new operation for all the components in the heirarchy.
* Aims at fostering Extensibility and Mantainability by performing "double dispatch"
*
* @brief Visitor Pattern can be exemplified by CityTourist that tours a City.
*/
#include <iostream>
class Paris;
class Delhi;
class CityVisitor
{
public:
virtual void visit(Paris &p) = 0;
virtual void visit(Delhi &d) = 0;
};
class CityTourist : public CityVisitor
{
void visit(Paris &p) override;
void visit(Delhi &p) override;
};
class City
{
public:
virtual void accept(CityVisitor *cv) = 0;
};
class Paris : public City
{
public:
void accept(CityVisitor *cv) override
{
cv->visit(*this);
}
};
class Delhi : public City
{
public:
void accept(CityVisitor *cv) override
{
cv->visit(*this);
}
};
void CityTourist::visit(Paris &p)
{
std::cout << "Touring Paris... \n";
}
void CityTourist::visit(Delhi &d)
{
std::cout << "Touring Delhi... \n";
}
int main()
{
Delhi dh;
Paris ps;
CityTourist cv;
dh.accept(&cv);
ps.accept(&cv);
return 0;
}