-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample_01.cpp
More file actions
118 lines (95 loc) · 2.89 KB
/
Copy pathConceptualExample_01.cpp
File metadata and controls
118 lines (95 loc) · 2.89 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
// ===========================================================================
// ConceptualExample_01.cpp // Command Pattern
// ===========================================================================
#include <iostream>
#include <memory>
#include <print>
#include <string>
#include <string_view>
namespace ConceptualExample_Command_Pattern {
/**
* Receiver classes contain business logic.
* They know how to perform all kinds of operations,
* associated with carrying out a command.
* In fact, any class may serve as a Receiver.
*/
class Receiver
{
public:
void action(std::string_view message) const
{
std::println("Action called with message {}", message);
}
};
/**
* The CommandBase interface declares a method for executing a command.
*/
class CommandBase
{
private:
std::shared_ptr<Receiver> m_receiver;
protected:
[[nodiscard]]
const std::shared_ptr<Receiver>& getReceiver() const { return m_receiver; }
public:
virtual ~CommandBase() = default;
explicit CommandBase(std::shared_ptr<Receiver> receiver)
: m_receiver{ std::move(receiver) }
{}
virtual void execute() const = 0;
};
/**
* Concrete implementation of CommandBase interface.
*/
class ConcreteCommand final : public CommandBase
{
private:
std::string m_data;
public:
ConcreteCommand(std::shared_ptr<Receiver> receiver, std::string data)
: CommandBase{ std::move(receiver) }, m_data{ std::move(data) }
{}
void execute() const override
{
const std::shared_ptr<Receiver>& receiver = getReceiver();
receiver->action(m_data);
}
};
/**
* Invoker executes Command
*/
class Invoker final
{
private:
// an invoker possesses the command exclusively
std::unique_ptr<CommandBase> m_command;
public:
void setCommand(std::unique_ptr<CommandBase> command)
{
m_command = std::move(command);
}
void executeCommand()
{
if (m_command) {
m_command->execute();
}
}
};
}
void test_conceptual_example_01() {
/**
* Variant 1:
* => classical Gang-of-Four implementation
* => explicit Command hierarchy
* => ideal for explaining the design pattern
*/
using namespace ConceptualExample_Command_Pattern;
auto receiver = std::make_shared<Receiver>();
auto command = std::make_unique<ConcreteCommand>(receiver, "Hello, world!");
Invoker invoker;
invoker.setCommand(std::move(command));
invoker.executeCommand();
}
// ===========================================================================
// End-of-File
// ===========================================================================