-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDynamicShapes.cpp
More file actions
186 lines (141 loc) · 4.8 KB
/
Copy pathDynamicShapes.cpp
File metadata and controls
186 lines (141 loc) · 4.8 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
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
// ===========================================================================
// DynamicShapes.cpp // Decorator Pattern
// ===========================================================================
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <memory>
namespace DynamicDecoration {
// corresponds to 'component'
class IShape
{
public:
virtual ~IShape() {}
virtual std::string draw() const = 0;
};
// corresponds to 'concrete component'
class Circle : public IShape {
private:
double m_radius;
public:
Circle() : m_radius{ 0.0 } {}
explicit Circle(double radius) : m_radius{ radius } {}
void resize(double factor) { m_radius *= factor; }
std::string draw() const override {
std::ostringstream oss;
oss << "A circle of radius " << std::fixed << m_radius;
return oss.str();
}
};
// corresponds to 'concrete component'
class Square : public IShape
{
private:
double m_side;
public:
Square() : m_side{ 0.0 } {}
explicit Square(double side) : m_side{ side } {}
virtual std::string draw() const override
{
std::ostringstream oss;
oss << "A square with side " << m_side;
return oss.str();
}
};
// ---------------------------------------------------------------------------
// corresponds to 'base decorator class'
class ShapeDecorator : public IShape {
protected:
std::shared_ptr<IShape> m_component;
public:
ShapeDecorator(const std::shared_ptr<IShape>& component)
: m_component{ component }
{}
// base decorator class delegates all work to the wrapped component
std::string draw() const override {
return m_component->draw();
}
};
// corresponds to 'concrete decorator class'
class ColoredShapeDecorator : public ShapeDecorator
{
private:
std::string m_color;
public:
ColoredShapeDecorator(const std::shared_ptr<IShape>& shape, const std::string& color)
: ShapeDecorator{ shape }, m_color{ color }
{}
virtual std::string draw() const override
{
std::ostringstream oss;
oss << ShapeDecorator::draw() << " has color " << m_color;
return oss.str();
}
};
// corresponds to another 'concrete decorator class'
class TransparentShapeDecorator : public ShapeDecorator
{
private:
uint8_t m_transparency;
public:
TransparentShapeDecorator(const std::shared_ptr<IShape>& shape, uint8_t transparency)
: ShapeDecorator{ shape }, m_transparency{ transparency }
{}
virtual std::string draw() const override
{
std::ostringstream oss;
oss << ShapeDecorator::draw() << " has "
<< (static_cast<double>(m_transparency) / 255.0) * 100.0
<< "% transparency";
return oss.str();
}
};
}
void test_dynamic_decoration_00() {
using namespace DynamicDecoration;
std::shared_ptr<IShape> circle{
std::make_shared<Circle>(0.5)
};
std::cout << circle->draw() << std::endl;
// "A circle of radius 0.5"
}
void test_dynamic_decoration_01() {
using namespace DynamicDecoration;
std::shared_ptr<IShape> circle{
std::make_shared<Circle>(0.5)
};
std::shared_ptr<IShape> redCircle{
std::make_shared<ColoredShapeDecorator>(circle, "red")
};
std::cout << redCircle->draw() << std::endl;
// "A circle of radius 0.500000 has color red"
}
void test_dynamic_decoration_02() {
using namespace DynamicDecoration;
std::shared_ptr<IShape> square{
std::make_shared<Square>(3.0)
};
std::shared_ptr<IShape> transparentSquare{
std::make_shared<TransparentShapeDecorator>(square, static_cast<uint8_t>(85))
};
std::cout << transparentSquare->draw() << std::endl;
// "A square with side 3 has 33.3333% transparency"
}
void test_dynamic_decoration_03() {
using namespace DynamicDecoration;
std::shared_ptr<IShape> circle{
std::make_shared<Circle>(15.0)
};
std::shared_ptr<IShape> greenCircle{
std::make_shared<ColoredShapeDecorator>(circle, "green")
};
std::shared_ptr<IShape> greenTransparentCircle{
std::make_shared<TransparentShapeDecorator>(greenCircle, static_cast<uint8_t>(50))
};
std::cout << greenTransparentCircle->draw() << std::endl;
// "A circle of radius 15.000000 has color green has 19.6078% transparency"
}
// ===========================================================================
// End-of-File
// ===========================================================================