-
Notifications
You must be signed in to change notification settings - Fork 124
Expand file tree
/
Copy pathmain.cc
More file actions
166 lines (157 loc) · 6.06 KB
/
main.cc
File metadata and controls
166 lines (157 loc) · 6.06 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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
/**
* EN: Strategy Design Pattern
*
* Intent: Lets you define a family of algorithms, put each of them into a
* separate class, and make their objects interchangeable.
*
* RU: Паттерн Стратегия
*
* Назначение: Определяет семейство схожих алгоритмов и помещает каждый из них в
* собственный класс, после чего алгоритмы можно взаимозаменять прямо во время
* исполнения программы.
*/
/**
* EN: 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.
*
* RU: Интерфейс Стратегии объявляет операции, общие для всех поддерживаемых
* версий некоторого алгоритма.
*
* Контекст использует этот интерфейс для вызова алгоритма, определённого
* Конкретными Стратегиями.
*/
class Strategy
{
public:
virtual ~Strategy() {}
virtual std::string DoAlgorithm(const std::vector<std::string> &data) const = 0;
};
/**
* EN: The Context defines the interface of interest to clients.
*
* RU: Контекст определяет интерфейс, представляющий интерес для клиентов.
*/
class Context
{
/**
* EN: @var Strategy 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.
*
* RU: @var Strategy Контекст хранит ссылку на один из объектов Стратегии.
* Контекст не знает конкретного класса стратегии. Он должен работать со
* всеми стратегиями через интерфейс Стратегии.
*/
private:
Strategy *strategy_;
/**
*
* EN: Usually, the Context accepts a strategy through the constructor, but
* also provides a setter to change it at runtime.
*
* RU: Обычно Контекст принимает стратегию через конструктор, а также
* предоставляет сеттер для её изменения во время выполнения.
*/
public:
Context(Strategy *strategy = nullptr) : strategy_(strategy)
{
}
~Context()
{
delete this->strategy_;
}
/**
* EN: Usually, the Context allows replacing a Strategy object at runtime.
*
* RU: Обычно Контекст позволяет заменить объект Стратегии во время
* выполнения.
*/
void set_strategy(Strategy *strategy)
{
delete this->strategy_;
this->strategy_ = strategy;
}
/**
* EN: The Context delegates some work to the Strategy object instead of
* implementing +multiple versions of the algorithm on its own.
*
* RU: Вместо того, чтобы самостоятельно реализовывать множественные версии
* алгоритма, Контекст делегирует некоторую работу объекту Стратегии.
*/
void DoSomeBusinessLogic() const
{
// ...
std::cout << "Context: Sorting data using the strategy (not sure how it'll do it)\n";
std::string result = this->strategy_->DoAlgorithm(std::vector<std::string>{"a", "e", "c", "b", "d"});
std::cout << result << "\n";
// ...
}
};
/**
* EN: Concrete Strategies implement the algorithm while following the base
* Strategy interface. The interface makes them interchangeable in the Context.
*
* RU: Конкретные Стратегии реализуют алгоритм, следуя базовому интерфейсу
* Стратегии. Этот интерфейс делает их взаимозаменяемыми в Контексте.
*/
class ConcreteStrategyA : public Strategy
{
public:
std::string DoAlgorithm(const std::vector<std::string> &data) const override
{
std::string result;
std::for_each(std::begin(data), std::end(data), [&result](const std::string &letter) {
result += letter;
});
std::sort(std::begin(result), std::end(result));
return result;
}
};
class ConcreteStrategyB : public Strategy
{
std::string DoAlgorithm(const std::vector<std::string> &data) const override
{
std::string result;
std::for_each(std::begin(data), std::end(data), [&result](const std::string &letter) {
result += letter;
});
std::sort(std::begin(result), std::end(result));
for (unsigned i = 0; i < result.size() / 2; i++)
{
std::swap(result[i], result[result.size() - i - 1]);
}
return result;
}
};
/**
* EN: 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.
*
* RU: Клиентский код выбирает конкретную стратегию и передаёт её в контекст.
* Клиент должен знать о различиях между стратегиями, чтобы сделать правильный
* выбор.
*/
void ClientCode()
{
Context *context = new Context(new ConcreteStrategyA);
std::cout << "Client: Strategy is set to normal sorting.\n";
context->DoSomeBusinessLogic();
std::cout << "\n";
std::cout << "Client: Strategy is set to reverse sorting.\n";
context->set_strategy(new ConcreteStrategyB);
context->DoSomeBusinessLogic();
delete context;
}
int main()
{
ClientCode();
return 0;
}