forked from caiorss/C-Cpp-Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
visitor1.cpp
189 lines (170 loc) · 4.67 KB
/
visitor1.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Brief: Simple visitor design pattern for processing heterogeneous derived classes.
// Author: Caio Rodrigues
//------------------------------------------------------------------
#include <iostream>
#include <cstdio>
#include <string>
#include <memory>
#include <functional>
// Forward reference
//class IVisitor;
class Circle;
class Square;
class Blob;
// File: IVisitor.h
// Visitor: The visitor interface encapsulates the operation performed
// on the class hierarchy. It should define a specific version operation
// (method visit) for each derived class.
class IVisitor{
public:
virtual ~IVisitor() = default;
virtual void visit(Circle& sh) = 0;
virtual void visit(Square& sh) = 0;
virtual void visit(Blob& sh) = 0;
};
// Shape interface - The base class must define the method accept
class IShape{
public:
virtual ~IShape() = default;
virtual void accept(IVisitor& v) = 0;
};
// ============ Concrete shapes ======= //
class Circle: public IShape{
public:
double radius;
Circle(double radius): radius(radius) { }
void accept(IVisitor& v) override {
v.visit(*this);
}
};
class Square: public IShape{
public:
double side;
Square(double side): side(side) { }
void accept(IVisitor& v) override {
v.visit(*this);
}
};
class Blob: public IShape{
public:
Blob(){}
void accept(IVisitor& v) override {
v.visit(*this);
}
};
// ======== Visitor Interface Implementation =============//
// Every visitor is a new operation to be performed on the
// class hierarchy
// Operation which prints name of the class hierarchy
class PrintNameVisitor: public IVisitor{
public:
void visit(Circle& sh) override
{
std::cout << " => Shape is a circle of radius = "
<< sh.radius
<< "\n";
}
void visit(Square& sh) override
{
std::cout << " => Shape is a square of side = "
<< sh.side
<< "\n";
}
void visit(Blob& ) override
{
std::cout << " => Shape is a blob with an undefined shape"
<< "\n";
}
};
class ComputeAreaVisitor: public IVisitor{
private:
double _area = 0.0;
public:
ComputeAreaVisitor() = default;
double getArea(){
return _area;
}
void visit(Circle& sh) override
{
// CircleArea = PI * radius^2
_area = 3.1415 * sh.radius * sh.radius;
}
void visit(Square& sh) override
{
_area = sh.side * sh.side;
}
void visit(Blob& ) override
{
_area = 100.0;
}
};
// Lambda function are used for adding new behavior to the object.
template<class Result>
class FunctionAdapter: public IVisitor{
template<typename T> using Func = std::function<Result (T& sh)>;
Result _res;
Func<Circle> _fn_circle;
Func<Square> _fn_square;
Func<Blob> _fn_blob;
public:
Result get(){
return _res;
}
FunctionAdapter(Func<Circle> fnCircle, Func<Square> fnSquare, Func<Blob> fnBlob)
: _res{}
, _fn_circle{fnCircle}
, _fn_square{fnSquare}
, _fn_blob{fnBlob}
{
}
void visit(Circle& sh) override { _res = _fn_circle(sh); }
void visit(Square& sh) override { _res = _fn_square(sh);}
void visit(Blob& sh) override { _res = _fn_blob(sh); }
};
int main()
{
// Sample shapes
auto s1 = Circle(3.0);
auto s2 = Square(4.0);
auto s3 = Blob();
std::cout << "===> Experiment 1: PrintNameVisitor " << "\n";
auto visitor1 = PrintNameVisitor();
s1.accept(visitor1);
s2.accept(visitor1);
s3.accept(visitor1);
std::cout << "===> Experiment 2: ComputeAreaVisitor " << "\n";
auto visitor2 = ComputeAreaVisitor();
s1.accept(visitor2);
std::cout << "Area of shape 1 = " << visitor2.getArea() << "\n";
s2.accept(visitor2);
std::cout << "Area of shape 2 = " << visitor2.getArea() << "\n";
s3.accept(visitor2);
std::cout << "Area of shape 3 = " << visitor2.getArea() << "\n";
std::cout << "===> Experiment 3: FunctionAdapter " << "\n";
// Creates operation to get shape name as string
auto visitor3 = FunctionAdapter<std::string>{
[](Circle& ){ return "circle"; },
[](Square& ){ return "square"; },
[](Blob& ){ return "blob"; },
};
s1.accept(visitor3);
std::cout << "Type of shape 1 = " << visitor3.get() << "\n";
s2.accept(visitor3);
std::cout << "Type of shape 2 = " << visitor3.get() << "\n";
s3.accept(visitor3);
std::cout << "Type of shape 3 = " << visitor3.get() << "\n";
std::cout << "===> Experiment 4: FunctionAdapter " << "\n";
// Creates operation to compute shape perimeter
auto visitor4 = FunctionAdapter<double>{
[](Circle& s){ return 2 * 3.1415 * s.radius * s.radius ; },
[](Square& s){ return 4.0 * s.side; },
[](Blob& ){ return -100.0; },
};
s1.accept(visitor4);
std::cout << "Perimeter of shape 1 = " << visitor4.get() << "\n";
s2.accept(visitor4);
std::cout << "Perimeter of shape 2 = " << visitor4.get() << "\n";
s3.accept(visitor4);
std::cout << "Perimeter of shape 3 = " << visitor4.get() << "\n";
return 0;
}