-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample01.cpp
More file actions
205 lines (165 loc) · 5.61 KB
/
Copy pathConceptualExample01.cpp
File metadata and controls
205 lines (165 loc) · 5.61 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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
// ===========================================================================
// ConceptualExample01.cpp // Mediator
// ===========================================================================
#define _CRTDBG_MAP_ALLOC
#include <cstdlib>
#include <crtdbg.h>
#ifdef _DEBUG
#ifndef DBG_NEW
#define DBG_NEW new ( _NORMAL_BLOCK , __FILE__ , __LINE__ )
#define new DBG_NEW
#endif
#endif // _DEBUG
#include <memory>
#include <print>
#include <string>
#include <string_view>
namespace ConceptualExample01 {
/**
* The Mediator interface declares a method used by components to notify the
* mediator about various events. The Mediator may react to these events and
* pass the execution to other components.
*/
enum class Event { A, B, C, D };
class ColleagueBase;
class MediatorBase {
public:
virtual ~MediatorBase() = default;
virtual void notify(const ColleagueBase& sender, Event event) = 0;
};
/**
* The Base Component provides the basic functionality of storing a mediator's
* instance inside component objects.
*/
class ColleagueBase
{
private:
std::string m_name;
protected:
std::weak_ptr<MediatorBase> m_mediator;
public:
ColleagueBase() = delete;
virtual ~ColleagueBase() = default;
explicit ColleagueBase(std::string name) : m_name{ std::move(name) } {}
void setMediator(std::shared_ptr<MediatorBase> mediator)
{
m_mediator = std::move(mediator);
}
[[nodiscard]]
std::string_view getName () const { return m_name; }
};
/**
* Concrete Components implement various functionality. They don't depend on
* other components. They also don't depend on any concrete mediator classes.
*/
class ConcreteColleagueA : public ColleagueBase
{
public:
explicit ConcreteColleagueA(std::string name) : ColleagueBase{ std::move(name) } {}
void operationA()
{
std::println("Component {} does operation A.", getName());
if (auto mediator = m_mediator.lock())
{
mediator->notify(*this, Event::A);
}
}
void operationB()
{
std::println("Component {} does operation B.", getName());
if (auto mediator = m_mediator.lock())
{
mediator->notify(*this, Event::B);
}
}
};
class ConcreteColleagueB : public ColleagueBase
{
public:
explicit ConcreteColleagueB(std::string name) : ColleagueBase{ std::move(name) } {}
void operationC()
{
std::println("Component {} does operation C.", getName());
if (auto mediator = m_mediator.lock())
{
mediator->notify(*this, Event::C);
}
}
void operationD()
{
std::println("Component {} does operation D.", getName());
if (auto mediator = m_mediator.lock())
{
mediator->notify(*this, Event::D);
}
}
};
/**
* Concrete Mediators implement cooperative behavior
* by coordinating several components.
*/
class ConcreteMediator : public MediatorBase
{
private:
std::shared_ptr<ConcreteColleagueA> m_componentA;
std::shared_ptr<ConcreteColleagueB> m_componentB;
public:
explicit ConcreteMediator(
std::shared_ptr<ConcreteColleagueA> colleagueA,
std::shared_ptr<ConcreteColleagueB> colleagueB)
: m_componentA{ std::move(colleagueA) },
m_componentB{ std::move(colleagueB) }
{}
public:
// static Factory method to ensure proper initialization of this mediator
static std::shared_ptr<ConcreteMediator> create(
std::shared_ptr<ConcreteColleagueA> cA,
std::shared_ptr<ConcreteColleagueB> cB)
{
auto mediator = std::make_shared<ConcreteMediator>(cA, cB);
cA->setMediator(mediator);
cB->setMediator(mediator);
return mediator;
}
public:
~ConcreteMediator() = default;
void notify(const ColleagueBase& sender, Event event) override
{
std::println("Notify => Sender: {}", sender.getName());
switch (event) {
case Event::A:
std::println("Mediator reacts on A and triggers following operations:");
m_componentB->operationC();
break;
case Event::D:
std::println("Mediator reacts on D and triggers following operations:");
m_componentA->operationB();
m_componentB->operationC();
break;
case Event::B:
case Event::C:
break;
}
}
};
static void clientCode()
{
auto cA = std::make_shared<ConcreteColleagueA>("Colleague A");
auto cB = std::make_shared<ConcreteColleagueB>("Colleague B");
auto mediator = ConcreteMediator::create(cA, cB);
std::println("Client triggers operation A.");
cA->operationA();
std::println();
std::println("Client triggers operation D.");
cB->operationD();
}
}
void test_conceptual_example01()
{
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
using namespace ConceptualExample01;
clientCode();
}
// ===========================================================================
// End-of-File
// ===========================================================================