-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvisitor.cpp
More file actions
89 lines (80 loc) · 2.35 KB
/
Copy pathvisitor.cpp
File metadata and controls
89 lines (80 loc) · 2.35 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
79
80
81
82
83
84
85
86
87
88
89
// Visitor Design Pattern
#include <iostream>
#include <memory>
// Forward Declare
class Visitor;
class Shape;
class Rectangle;
class Circle;
class Triangle;
// When to use:
/*
- Needs common operation on a group or structure of complex objects (e.g. a tree)
- Clean up business logic (refocus a class on data rather than jobs)
*/
class Visitor { // abstract visitor
public:
virtual void visitCircle(const Circle* circle) const = 0;
virtual void visitRectangle(const Rectangle* rectangle) const = 0;
virtual void visitTriangle(const Triangle* triangle) const = 0;
};
class Shape {
public:
virtual ~Shape() = default;
virtual void accept(const Visitor& visitor) = 0;
};
class Circle : public Shape {
public:
Circle(double radius) : radius_(radius) {}
void accept(const Visitor& visitor) {
visitor.visitCircle(this);
}
double getRadius() const { return radius_; }
private:
double radius_;
};
class Rectangle : public Shape {
public:
Rectangle(double length, double width) : length_(length), width_(width) {}
void accept(const Visitor& visitor) {
visitor.visitRectangle(this);
}
double getLength() const { return length_; }
double getWidth() const { return width_; }
private:
double length_;
double width_;
};
class Triangle : public Shape {
public:
Triangle(double height, double width) : height_(height), width_(width) {}
void accept(const Visitor& visitor) {
visitor.visitTriangle(this);
}
double getHeight() const { return height_; }
double getWidth() const { return width_; }
private:
double height_;
double width_;
};
class CalculateArea : public Visitor { // single responsibility class!!!
public:
CalculateArea() {}
void visitCircle(const Circle* circle) const override {
std::cout << "Area of a circle is " << 3.14 * circle->getRadius() << "\n";
}
void visitRectangle(const Rectangle* rectangle) const override {
std::cout << "Area of rectangle is " << rectangle->getLength() * rectangle->getWidth() << "\n";
}
void visitTriangle(const Triangle* triangle) const override {
std::cout << "Area of triangle is " << triangle->getHeight() * 0.5 * triangle->getWidth() << "\n";
}
};
int main() {
Circle circle1(5.6);
Rectangle square(8, 8);
std::unique_ptr<Visitor> findArea = std::make_unique<CalculateArea>(); // safe memory handling
circle1.accept(*findArea);
square.accept(*findArea);
return 0;
}