-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExampleModern.cpp
More file actions
160 lines (130 loc) · 4.28 KB
/
ConceptualExampleModern.cpp
File metadata and controls
160 lines (130 loc) · 4.28 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
// ===========================================================================
// ConceptualExampleModern.cpp // Strategy Pattern
// ===========================================================================
#include <algorithm>
#include <functional>
#include <memory>
#include <print>
#include <string>
#include <vector>
namespace StrategyConceptualExampleModern {
/**
* The Strategy Interface declares operations
* common to all supported versions of some algorithm.
*
* The Context uses this interface to call the algorithm
* defined by Concrete Strategies.
*
* In Modern C++ the Strategy Interface may be defined through a std::function type.
*/
using Strategy = std::function<std::string(const std::vector<std::string>&)>;
/**
* Concrete strategies implement the algorithm
* based upon the std::function type definition.
* The definition makes them interchangeable in the Context.
*/
static std::string doAlgorithmA(const std::vector<std::string>& data)
{
std::string result{};
std::for_each(
data.begin(),
data.end(),
[&](const std::string& letter) {
result += letter;
}
);
std::sort(
result.begin(),
result.end()
);
return result;
}
static std::string doAlgorithmB(const std::vector<std::string>& data)
{
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 an object implementing one of the available strategies,
* in our case an std::function<> object.
* The Context doesn't have any knowledge of the concrete implementation of the wrapped function.
* It can just call this function via the std::function<> object.
*/
private:
Strategy m_strategy;
/**
* Usually, the Context accepts a strategy through the constructor,
* but also provides setter methods to change the strategy at runtime.
*/
public:
// c'tor(s), d'tor
Context(Strategy&& strategy) : m_strategy{ std::move(strategy) } {}
Context(Strategy& strategy) : m_strategy{ strategy } {}
~Context() {}
/**
* Usually, the Context allows replacing a Strategy object at runtime.
*/
void setStrategy(Strategy&& strategy)
{
m_strategy = std::move(strategy);
}
void setStrategy(Strategy& strategy)
{
m_strategy = 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(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{ doAlgorithmA };
context.doSomeBusinessLogic();
std::println();
std::println("Client: Strategy is set to 'Reverse Sorting':");
context.setStrategy(doAlgorithmB);
context.doSomeBusinessLogic();
}
}
void test_conceptual_example_modern()
{
using namespace StrategyConceptualExampleModern;
clientCode();
}
// ===========================================================================
// End-of-File
// ===========================================================================