Skip to content

Commit afddbcd

Browse files
authored
Merge pull request #8 from hextriclosan/main
Migrate to C++17; refactor Strategy pattern
2 parents 846eb95 + c2dacdd commit afddbcd

File tree

2 files changed

+27
-41
lines changed

2 files changed

+27
-41
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ It contains C++ examples for all classic GoF design patterns. Each pattern inclu
1010

1111
## Requirements
1212

13-
The examples were written as cross platform console application using c++11. It means that you should be able to compile and execute those examples with any recent compiler.
13+
The examples were written as cross platform console application using c++17. It means that you should be able to compile and execute those examples with any recent compiler.
1414

1515
we recommend working with Visual Studio Code because it is a lightweight and cross-platform tool .It is a very complete IDE and is available for free (https://code.visualstudio.com/). You may need to install c++ extension and the compiler you prefer (The extension is still in preview and its focus is code editing, navigation, and debugging support for C and C++). For more information on how to use VSCode with c++ refer to: https://code.visualstudio.com/docs/languages/cpp .
1616

@@ -23,7 +23,7 @@ For code execution in VSCode you will need to set up your task first. An example
2323
{
2424
"label": "build",
2525
"type": "shell",
26-
"command": "g++ -g -std=c++11 Conceptual/main.cc -o main",
26+
"command": "g++ -g -std=c++17 Conceptual/main.cc -o main",
2727
"group":{
2828
"kind": "build",
2929
"isDefault": true

src/Strategy/Conceptual/main.cc

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
class Strategy
3333
{
3434
public:
35-
virtual ~Strategy() {}
36-
virtual std::string DoAlgorithm(const std::vector<std::string> &data) const = 0;
35+
virtual ~Strategy() = default;
36+
virtual std::string doAlgorithm(std::string_view data) const = 0;
3737
};
3838

3939
/**
@@ -54,7 +54,7 @@ class Context
5454
* всеми стратегиями через интерфейс Стратегии.
5555
*/
5656
private:
57-
Strategy *strategy_;
57+
std::unique_ptr<Strategy> strategy_;
5858
/**
5959
*
6060
* EN: Usually, the Context accepts a strategy through the constructor, but
@@ -64,23 +64,18 @@ class Context
6464
* предоставляет сеттер для её изменения во время выполнения.
6565
*/
6666
public:
67-
Context(Strategy *strategy = nullptr) : strategy_(strategy)
67+
explicit Context(std::unique_ptr<Strategy> &&strategy = {}) : strategy_(std::move(strategy))
6868
{
6969
}
70-
~Context()
71-
{
72-
delete this->strategy_;
73-
}
7470
/**
7571
* EN: Usually, the Context allows replacing a Strategy object at runtime.
7672
*
7773
* RU: Обычно Контекст позволяет заменить объект Стратегии во время
7874
* выполнения.
7975
*/
80-
void set_strategy(Strategy *strategy)
76+
void set_strategy(std::unique_ptr<Strategy> &&strategy)
8177
{
82-
delete this->strategy_;
83-
this->strategy_ = strategy;
78+
strategy_ = std::move(strategy);
8479
}
8580
/**
8681
* EN: The Context delegates some work to the Strategy object instead of
@@ -89,13 +84,15 @@ class Context
8984
* RU: Вместо того, чтобы самостоятельно реализовывать множественные версии
9085
* алгоритма, Контекст делегирует некоторую работу объекту Стратегии.
9186
*/
92-
void DoSomeBusinessLogic() const
87+
void doSomeBusinessLogic() const
9388
{
94-
// ...
95-
std::cout << "Context: Sorting data using the strategy (not sure how it'll do it)\n";
96-
std::string result = this->strategy_->DoAlgorithm(std::vector<std::string>{"a", "e", "c", "b", "d"});
97-
std::cout << result << "\n";
98-
// ...
89+
if (strategy_) {
90+
std::cout << "Context: Sorting data using the strategy (not sure how it'll do it)\n";
91+
std::string result = strategy_->doAlgorithm("aecbd");
92+
std::cout << result << "\n";
93+
} else {
94+
std::cout << "Context: Strategy isn't set\n";
95+
}
9996
}
10097
};
10198

@@ -109,30 +106,20 @@ class Context
109106
class ConcreteStrategyA : public Strategy
110107
{
111108
public:
112-
std::string DoAlgorithm(const std::vector<std::string> &data) const override
109+
std::string doAlgorithm(std::string_view data) const override
113110
{
114-
std::string result;
115-
std::for_each(std::begin(data), std::end(data), [&result](const std::string &letter) {
116-
result += letter;
117-
});
111+
std::string result(data);
118112
std::sort(std::begin(result), std::end(result));
119113

120114
return result;
121115
}
122116
};
123117
class ConcreteStrategyB : public Strategy
124118
{
125-
std::string DoAlgorithm(const std::vector<std::string> &data) const override
119+
std::string doAlgorithm(std::string_view data) const override
126120
{
127-
std::string result;
128-
std::for_each(std::begin(data), std::end(data), [&result](const std::string &letter) {
129-
result += letter;
130-
});
131-
std::sort(std::begin(result), std::end(result));
132-
for (int i = 0; i < result.size() / 2; i++)
133-
{
134-
std::swap(result[i], result[result.size() - i - 1]);
135-
}
121+
std::string result(data);
122+
std::sort(std::begin(result), std::end(result), std::greater<>());
136123

137124
return result;
138125
}
@@ -147,20 +134,19 @@ class ConcreteStrategyB : public Strategy
147134
* выбор.
148135
*/
149136

150-
void ClientCode()
137+
void clientCode()
151138
{
152-
Context *context = new Context(new ConcreteStrategyA);
139+
Context context(std::make_unique<ConcreteStrategyA>());
153140
std::cout << "Client: Strategy is set to normal sorting.\n";
154-
context->DoSomeBusinessLogic();
141+
context.doSomeBusinessLogic();
155142
std::cout << "\n";
156143
std::cout << "Client: Strategy is set to reverse sorting.\n";
157-
context->set_strategy(new ConcreteStrategyB);
158-
context->DoSomeBusinessLogic();
159-
delete context;
144+
context.set_strategy(std::make_unique<ConcreteStrategyB>());
145+
context.doSomeBusinessLogic();
160146
}
161147

162148
int main()
163149
{
164-
ClientCode();
150+
clientCode();
165151
return 0;
166152
}

0 commit comments

Comments
 (0)