-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathRealWorldExample.cpp
More file actions
207 lines (159 loc) · 5.57 KB
/
RealWorldExample.cpp
File metadata and controls
207 lines (159 loc) · 5.57 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// ===========================================================================
// RealWorldExample.cpp // Strategy Pattern
// ===========================================================================
#include <memory>
#include <print>
#include <string>
#include <vector>
namespace StrategyRealWorldExample {
// =======================================================================
/**
* Common Base Interface for all visual widgets.
* This interface is not related directly to the Strategy Design Pattern.
*/
class Shape
{
public:
virtual ~Shape() = default;
virtual void draw() const = 0;
};
// =======================================================================
/**
* The DrawCircleStrategy and DrawRectangleStrategy interface declare operations
* common to all supported versions of some drawing algorithms.
*/
class Circle;
class Rectangle;
class DrawCircleStrategy
{
public:
virtual ~DrawCircleStrategy() {}
virtual void draw(const Circle&) const = 0;
};
class DrawRectangleStrategy
{
public:
virtual ~DrawRectangleStrategy() {}
virtual void draw(const Rectangle&) const = 0;
};
// ===========================================================================
/**
* The Circle uses the DrawCircleStrategy interface to call the algorithm
* defined by some DrawCircleStrategy support.
*/
class Circle : public Shape
{
public:
explicit Circle(double radius, std::unique_ptr<DrawCircleStrategy> drawer)
: m_radius{ radius }, m_drawer{ std::move(drawer) }
{
}
void setDrawCircleStrategy(std::unique_ptr<DrawCircleStrategy> drawer)
{
m_drawer = std::move(drawer);
}
void draw() const override
{
m_drawer->draw(*this);
}
double radius() const { return m_radius; }
private:
std::unique_ptr<DrawCircleStrategy> m_drawer;
double m_radius;
};
class Rectangle : public Shape
{
public:
explicit Rectangle(double width, double height, std::unique_ptr<DrawRectangleStrategy> drawer)
: m_width{ width }, m_height{ height }, m_drawer{ std::move(drawer) }
{
}
void setDrawRectangleStrategy(std::unique_ptr<DrawRectangleStrategy> strategy)
{
m_drawer = std::move(strategy);
}
void draw() const override
{
m_drawer->draw(*this);
}
double width() const { return m_width; }
double height() const { return m_height; }
private:
std::unique_ptr<DrawRectangleStrategy> m_drawer;
double m_width;
double m_height;
};
// ===========================================================================
/**
* Concrete strategies implement the algorithm
* while following the Base Strategy interface.
* The interface makes them interchangeable in the clients (Visual Widgets).
*/
class Win32CircleStrategy : public DrawCircleStrategy
{
public:
explicit Win32CircleStrategy() {};
void draw(const Circle& circle) const override {
std::println("Drawing a Circle using the Win32 Framework");
}
private:
/* Drawing related data members, e.g. colors, textures, ... */
};
class Win32RectangleStrategy : public DrawRectangleStrategy
{
public:
explicit Win32RectangleStrategy() {};
void draw(Rectangle const& rectangle) const override {
std::println("Drawing a Rectangle using the Rectangle Framework");
}
private:
/* Drawing related data members, e.g. colors, textures, ... */
};
// ===========================================================================
/**
* The client code picks a concrete strategy and passes it to the context.
* The client should be aware of the differences between strategies
* in order to make the right choice.
*/
static void clientCode(const std::vector<std::unique_ptr<Shape>>& shapes)
{
for (auto const& shape : shapes)
{
shape->draw();
}
}
}
void test_realworld_example()
{
using namespace StrategyRealWorldExample;
using Shapes = std::vector<std::unique_ptr<Shape>>;
Shapes shapes{};
// creating strategy objects based upon the Win32 drawing strategy
std::unique_ptr<DrawCircleStrategy> strategy1 {
std::make_unique<Win32CircleStrategy>()
};
std::unique_ptr<DrawRectangleStrategy> strategy2{
std::make_unique<Win32RectangleStrategy>()
};
std::unique_ptr<DrawCircleStrategy> strategy3{
std::make_unique<Win32CircleStrategy>()
};
// creating visual widgets using strategy objects
std::unique_ptr<Circle> upc{ std::make_unique<Circle>(
3.0, std::move(strategy1))
};
std::unique_ptr<Rectangle> upr{ std::make_unique<Rectangle>(
4.0, 5.0, std::move(strategy2))
};
std::unique_ptr<Circle> upc2{ std::make_unique<Circle>(
6.0, std::move(strategy3))
};
// filling container with visual widgets
shapes.push_back(std::move(upc));
shapes.push_back(std::move(upr));
shapes.push_back(std::move(upc2));
clientCode(shapes);
}
// ===========================================================================
// End-of-File
// ===========================================================================