-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStaticStrategyExample.cpp
More file actions
188 lines (149 loc) · 5.01 KB
/
StaticStrategyExample.cpp
File metadata and controls
188 lines (149 loc) · 5.01 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
// ===========================================================================
// StaticStrategyExample.cpp // Static Strategy Pattern Example
// ===========================================================================
#include <initializer_list>
#include <iostream>
#include <memory>
#include <print>
#include <sstream>
#include <string>
// =====================================================================
// dynamic strategy pattern example
namespace DynamicStrategyPatternExample {
enum class Format { Markdown, Html };
struct IListStrategy
{
virtual ~IListStrategy() {}
virtual void start(std::ostringstream& oss) = 0;
virtual void end(std::ostringstream& oss) = 0;
virtual void add_item(std::ostringstream& oss, const std::string& item) = 0;
};
struct MarkdownListStrategy : public IListStrategy
{
void start(std::ostringstream& oss) override {};
void end(std::ostringstream& oss) override {};
void add_item(std::ostringstream& oss, const std::string& item) override {
oss << " - " << item << std::endl;
}
};
struct HtmlListStrategy : public IListStrategy
{
void start(std::ostringstream& oss) override {
oss << "<ul>" << std::endl;
}
void end(std::ostringstream& oss) override {
oss << "</ul>" << std::endl;
}
void add_item(std::ostringstream& oss, const std::string& item) override {
oss << "\t<li>" << item << "</li>" << std::endl;
}
};
class TextProcessor
{
private:
std::unique_ptr<IListStrategy> m_list_strategy;
std::ostringstream m_oss;
public:
void clear() {
m_oss.str("");
m_oss.clear();
}
void append_list(std::initializer_list<std::string> items) {
m_list_strategy->start(m_oss);
for (const auto& item : items) {
m_list_strategy->add_item(m_oss, item);
}
m_list_strategy->end(m_oss);
}
void set_output_format(Format format) {
switch (format)
{
case Format::Markdown:
m_list_strategy = std::make_unique<MarkdownListStrategy>();
break;
case Format::Html:
m_list_strategy = std::make_unique<HtmlListStrategy>();
break;
}
}
std::string str() { return m_oss.str(); }
};
}
void test_dynamic_strategy_example ()
{
using namespace DynamicStrategyPatternExample;
// markdown
TextProcessor tp{};
tp.set_output_format(Format::Markdown);
tp.append_list({ "foo", "bar", "baz" });
std::println("{}", tp.str());
// html
tp.clear();
tp.set_output_format(Format::Html);
tp.append_list({ "foo", "bar", "baz" });
std::println("{}", tp.str());
}
// =====================================================================
// static strategy pattern example
namespace StaticStrategyPatternExample {
template <typename TList>
concept ListRequirements = requires(TList& list, std::ostringstream& oss, const std::string& item)
{
{ list.start(oss) } -> std::same_as<void>;
{ list.end(oss) } -> std::same_as<void>;
{ list.add_item(oss, item) } -> std::same_as<void>;
};
template<typename TList>
requires ListRequirements<TList>
class TextProcessorEx
{
private:
TList m_list;
std::ostringstream m_oss;
public:
void append_list(std::initializer_list<std::string> items)
{
m_list.start(m_oss);
for (const auto& item : items) {
m_list.add_item(m_oss, item);
}
m_list.end(m_oss);
}
std::string str() const { return m_oss.str(); }
};
struct MarkdownListStrategyEx
{
void start(std::ostringstream& oss) {};
void end(std::ostringstream& oss) {};
void add_item(std::ostringstream& oss, const std::string& item) {
oss << " - " << item << std::endl;
}
};
struct HtmlListStrategyEx
{
void start(std::ostringstream& oss) {
oss << "<ul>" << std::endl;
}
void end(std::ostringstream& oss) {
oss << "</ul>" << std::endl;
}
void add_item(std::ostringstream& oss, const std::string& item) {
oss << "\t<li>" << item << "</li>" << std::endl;
}
};
}
void test_static_strategy_example ()
{
using namespace StaticStrategyPatternExample;
// Markdown
TextProcessorEx<MarkdownListStrategyEx> tp1{};
tp1.append_list({ "foo", "bar", "baz" });
std::println("{}", tp1.str());
// <Html
TextProcessorEx<HtmlListStrategyEx> tp2{};
tp2.append_list({ "foo", "bar", "baz" });
std::println("{}", tp2.str());
}
// ===========================================================================
// End-of-File
// ===========================================================================