-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample.cpp
More file actions
176 lines (143 loc) · 4.7 KB
/
ConceptualExample.cpp
File metadata and controls
176 lines (143 loc) · 4.7 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
// ===========================================================================
// ConceptualExample.cpp // Strategy Pattern
// ===========================================================================
#include <algorithm>
#include <memory>
#include <string>
#include <vector>
#include <print>
namespace StrategyConceptualExample {
/**
* The StrategyBase interface declares operations
* common to all supported versions of some algorithm.
*
* The Context uses this interface to call the algorithm
* defined by Concrete Strategies.
*/
class StrategyBase
{
public:
virtual ~StrategyBase() {}
virtual std::string doAlgorithm(const std::vector<std::string>& data) const = 0;
virtual std::string getName() const = 0;
};
// =======================================================================
/**
* Concrete strategies implement the algorithm
* while following the base Strategy interface.
* The interface makes them interchangeable in the Context.
*/
class ConcreteStrategyA : public StrategyBase
{
public:
std::string getName() const override {
return { "Normal Sorting" };
}
std::string doAlgorithm(const std::vector<std::string>& data) const override
{
std::string result{};
std::for_each(
data.begin(),
data.end(),
[&](const std::string& letter) {
result += letter;
}
);
std::sort(
result.begin(),
result.end()
);
return result;
}
};
class ConcreteStrategyB : public StrategyBase
{
public:
std::string getName() const override {
return { "Reverse Sorting" };
}
std::string doAlgorithm(const std::vector<std::string>& data) const override
{
std::string result;
std::for_each(
data.begin(),
data.end(),
[&](const std::string& letter) {
result += letter;
}
);
std::sort(
result.begin(),
result.end()
);
std::reverse(
result.begin(),
result.end()
);
return result;
}
};
/**
* The Context uses the strategy interface
*/
class Context
{
/**
* The Context maintains a reference to one of the Strategy objects.
* The Context does not know the concrete class of a strategy.
* It should work with all strategies via the 'Strategy' interface.
*/
private:
std::unique_ptr<StrategyBase> m_strategy;
/**
* Usually, the Context accepts a strategy through the constructor,
* but also provides a setter to change it at runtime.
*/
public:
Context(std::unique_ptr<StrategyBase> strategy)
: m_strategy{ std::move(strategy) }
{}
~Context() {}
/**
* Usually, the Context allows replacing a Strategy object at runtime.
*/
void setStrategy(std::unique_ptr<StrategyBase> strategy)
{
m_strategy = std::move(strategy);
}
/**
* The Context delegates some work to the Strategy object instead of
* implementing multiple versions of the algorithm on its own.
*/
void doSomeBusinessLogic() const
{
std::vector<std::string> someStrings{ "A", "E", "C", "B", "D" };
std::println("Context: Sorting data ...");
std::string result{ m_strategy->doAlgorithm(someStrings) };
std::println("Result: {}", result);
}
};
/**
* 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()
{
std::println("Client: Strategy is set to 'Normal Sorting':");
Context context{ std::make_unique<ConcreteStrategyA>() };
context.doSomeBusinessLogic();
std::println();
std::println("Client: Strategy is set to 'Reverse Sorting':");
context.setStrategy(std::make_unique<ConcreteStrategyB>());
context.doSomeBusinessLogic();
}
}
void test_conceptual_example()
{
using namespace StrategyConceptualExample;
clientCode();
}
// ===========================================================================
// End-of-File
// ===========================================================================