1
+ #include < string>
2
+ #include < memory>
3
+ #include < iostream>
4
+
5
+ using namespace std ;
6
+
7
+ struct IShape
8
+ {
9
+ virtual auto draw () -> void = 0;
10
+ virtual auto description () -> std::string = 0;
11
+ virtual ~IShape () = default ;
12
+ };
13
+
14
+ // Component class
15
+ class Square : public IShape
16
+ {
17
+ public:
18
+ auto draw () -> void
19
+ {
20
+ std::cout << " => Draw square" << std::endl;
21
+ }
22
+ auto description () -> std::string
23
+ {
24
+ return " square" ;
25
+ }
26
+ };
27
+
28
+ // Component class
29
+ class Triangle : public IShape
30
+ {
31
+ public:
32
+ auto draw () -> void
33
+ {
34
+ std::cout << " => Draw tringle" << std::endl;
35
+ }
36
+ auto description () -> std::string
37
+ {
38
+ return " triangle" ;
39
+ }
40
+ };
41
+
42
+ // Decorator 1 => Draw shape with color
43
+ class ColorDecorator : public IShape
44
+ {
45
+ public:
46
+ ColorDecorator (std::shared_ptr<IShape> shape)
47
+ : m_shape(shape) {}
48
+ auto draw () -> void
49
+ {
50
+ // Save color: push()
51
+ std::cout << " => [ColorDecorator] Draw object with color blue" << std::endl;
52
+ m_shape->draw ();
53
+ // Restore color: pop()
54
+ }
55
+ auto description () -> std::string
56
+ {
57
+ return m_shape->description () + " ; color = " + m_color;
58
+ }
59
+ // Return a reference to itself (ColorDecorator&)
60
+ auto setColor (const std::string &color) -> decltype(*this ) &
61
+ {
62
+ m_color = color;
63
+ return *this ;
64
+ }
65
+
66
+ private:
67
+ // The decorator owns the decorated object
68
+ std::shared_ptr<IShape> m_shape;
69
+ std::string m_color = " blue" ;
70
+ };
71
+
72
+ class PositionDecorator : public IShape
73
+ {
74
+ public:
75
+ PositionDecorator (std::shared_ptr<IShape> shape)
76
+ : m_shape(shape) {}
77
+
78
+ auto draw () -> void
79
+ {
80
+ // Save transformation matrix: pushMatrix()
81
+ std::cout << " => [PositionDecorator] Draw object at x = "
82
+ << m_x << " ; y = " << m_y << std::endl;
83
+ m_shape->draw ();
84
+ // Restore transformation matrix: popMatrix()
85
+ }
86
+ auto description () -> std::string
87
+ {
88
+ return m_shape->description () + " ; position x = " + std::to_string (m_x) + " , y = " + std::to_string (m_y);
89
+ }
90
+ auto setPosition (double x, double y) -> PositionDecorator &
91
+ {
92
+ m_x = x, m_y = y;
93
+ return *this ;
94
+ }
95
+
96
+ private:
97
+ // The decorator owns the decorated object
98
+ std::shared_ptr<IShape> m_shape;
99
+ int m_x = 0 , m_y = 0 ;
100
+ };
101
+
102
+ int main ()
103
+ {
104
+ std::puts (" \n ======>> Experiment 1 <<===========" );
105
+ auto shape = std::make_shared<ColorDecorator>(std::make_shared<Square>());
106
+ shape->setColor (" yellow" );
107
+ shape->draw ();
108
+
109
+ std::puts (" \n ======>> Experiment 2 <<===========" );
110
+ auto observerd = std::shared_ptr<IShape>{nullptr };
111
+
112
+ auto shapeWithColorAndPosition =
113
+ [&observerd] {
114
+ auto shape = std::make_shared<Triangle>();
115
+ observerd = shape;
116
+ auto shapeColored = std::make_shared<ColorDecorator>(shape);
117
+ shapeColored->setColor (" white" );
118
+ auto shapePos = std::make_shared<PositionDecorator>(shapeColored);
119
+ return shapePos;
120
+ }();
121
+
122
+ shapeWithColorAndPosition->setPosition (100 , 20 );
123
+ shapeWithColorAndPosition->draw ();
124
+ std::cout << " DESCRIPTION = "
125
+ << shapeWithColorAndPosition->description () << std::endl;
126
+
127
+ std::cout << " [INFO] observed shape = " << observerd->description () << std::endl;
128
+ }
0 commit comments