Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ It contains C++ examples for all classic GoF design patterns. Each pattern inclu

## Requirements

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.
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.

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 .

Expand All @@ -23,7 +23,7 @@ For code execution in VSCode you will need to set up your task first. An example
{
"label": "build",
"type": "shell",
"command": "g++ -g -std=c++11 Conceptual/main.cc -o main",
"command": "g++ -g -std=c++17 Conceptual/main.cc -o main",
"group":{
"kind": "build",
"isDefault": true
Expand Down
64 changes: 25 additions & 39 deletions src/Strategy/Conceptual/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
class Strategy
{
public:
virtual ~Strategy() {}
virtual std::string DoAlgorithm(const std::vector<std::string> &data) const = 0;
virtual ~Strategy() = default;
virtual std::string doAlgorithm(std::string_view data) const = 0;
};

/**
Expand All @@ -54,7 +54,7 @@ class Context
* всеми стратегиями через интерфейс Стратегии.
*/
private:
Strategy *strategy_;
std::unique_ptr<Strategy> strategy_;
/**
*
* EN: Usually, the Context accepts a strategy through the constructor, but
Expand All @@ -64,23 +64,18 @@ class Context
* предоставляет сеттер для её изменения во время выполнения.
*/
public:
Context(Strategy *strategy = nullptr) : strategy_(strategy)
explicit Context(std::unique_ptr<Strategy> &&strategy = {}) : strategy_(std::move(strategy))
{
}
~Context()
{
delete this->strategy_;
}
/**
* EN: Usually, the Context allows replacing a Strategy object at runtime.
*
* RU: Обычно Контекст позволяет заменить объект Стратегии во время
* выполнения.
*/
void set_strategy(Strategy *strategy)
void set_strategy(std::unique_ptr<Strategy> &&strategy)
{
delete this->strategy_;
this->strategy_ = strategy;
strategy_ = std::move(strategy);
}
/**
* EN: The Context delegates some work to the Strategy object instead of
Expand All @@ -89,13 +84,15 @@ class Context
* RU: Вместо того, чтобы самостоятельно реализовывать множественные версии
* алгоритма, Контекст делегирует некоторую работу объекту Стратегии.
*/
void DoSomeBusinessLogic() const
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";
// ...
if (strategy_) {
std::cout << "Context: Sorting data using the strategy (not sure how it'll do it)\n";
std::string result = strategy_->doAlgorithm("aecbd");
std::cout << result << "\n";
} else {
std::cout << "Context: Strategy isn't set\n";
}
}
};

Expand All @@ -109,30 +106,20 @@ class Context
class ConcreteStrategyA : public Strategy
{
public:
std::string DoAlgorithm(const std::vector<std::string> &data) const override
std::string doAlgorithm(std::string_view data) const override
{
std::string result;
std::for_each(std::begin(data), std::end(data), [&result](const std::string &letter) {
result += letter;
});
std::string result(data);
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 doAlgorithm(std::string_view 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 (int i = 0; i < result.size() / 2; i++)
{
std::swap(result[i], result[result.size() - i - 1]);
}
std::string result(data);
std::sort(std::begin(result), std::end(result), std::greater<>());

return result;
}
Expand All @@ -147,20 +134,19 @@ class ConcreteStrategyB : public Strategy
* выбор.
*/

void ClientCode()
void clientCode()
{
Context *context = new Context(new ConcreteStrategyA);
Context context(std::make_unique<ConcreteStrategyA>());
std::cout << "Client: Strategy is set to normal sorting.\n";
context->DoSomeBusinessLogic();
context.doSomeBusinessLogic();
std::cout << "\n";
std::cout << "Client: Strategy is set to reverse sorting.\n";
context->set_strategy(new ConcreteStrategyB);
context->DoSomeBusinessLogic();
delete context;
context.set_strategy(std::make_unique<ConcreteStrategyB>());
context.doSomeBusinessLogic();
}

int main()
{
ClientCode();
clientCode();
return 0;
}