-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathConceptualExample02.cpp
More file actions
175 lines (144 loc) · 5.34 KB
/
Copy pathConceptualExample02.cpp
File metadata and controls
175 lines (144 loc) · 5.34 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
// ===========================================================================
// ConceptualExample02.cpp // Mediator
// ===========================================================================
#include <iostream>
#include <string>
#include <memory>
namespace ConceptualExample02 {
/**
* 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.
*/
class ColleagueBase;
class MediatorBaseAlternate
{
public:
virtual ~MediatorBaseAlternate() = default;
virtual void notify(const std::shared_ptr<ColleagueBase>& sender) const = 0;
};
/**
* The Base Component provides the basic functionality of storing a mediator's
* instance inside component objects.
*/
class ColleagueBase
{
protected:
std::weak_ptr<MediatorBaseAlternate> m_mediator;
public:
ColleagueBase() {}
ColleagueBase(const std::shared_ptr<MediatorBaseAlternate>& mediator)
: m_mediator{ mediator }
{}
void setMediator(const std::shared_ptr<MediatorBaseAlternate>& mediator)
{
m_mediator = mediator;
}
};
/**
* 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 std::enable_shared_from_this<ConcreteColleagueA> {
public:
void operationA()
{
std::cout << "Component Colleague A does operation A." << std::endl;
std::shared_ptr<MediatorBaseAlternate> sp = m_mediator.lock();
if (sp != nullptr) {
sp->notify(shared_from_this());
}
}
void operationX()
{
std::cout << "Component Colleague A does operation X." << std::endl;
}
};
class ConcreteColleagueB : public ColleagueBase, public std::enable_shared_from_this<ConcreteColleagueB>
{
public:
void operationB()
{
std::cout << "Component Colleague B does operation B." << std::endl;
std::shared_ptr<MediatorBaseAlternate> sp = m_mediator.lock();
if (sp != nullptr) {
sp->notify(shared_from_this());
}
}
void operationY()
{
std::cout << "Component Colleague B does operation Y." << std::endl;
}
void operationZ()
{
std::cout << "Component Colleague B does operation Z." << std::endl;
}
};
/**
* Concrete Mediators implement cooperative behavior
* by coordinating several components.
*/
class ConcreteMediator : public MediatorBaseAlternate, public std::enable_shared_from_this<ConcreteMediator>
{
private:
std::shared_ptr<ConcreteColleagueA> m_componentA;
std::shared_ptr<ConcreteColleagueB> m_componentB;
public:
ConcreteMediator(const std::shared_ptr<ConcreteColleagueA>& c1, const std::shared_ptr<ConcreteColleagueB>& c2)
: m_componentA{ c1 }, m_componentB{ c2 }
{}
~ConcreteMediator() {}
void setConcreteColleagueA()
{
m_componentA->setMediator(shared_from_this());
}
void setConcreteColleagueB()
{
m_componentB->setMediator(shared_from_this());
}
void setConcreteColleagues()
{
m_componentA->setMediator(shared_from_this());
m_componentB->setMediator(shared_from_this());
}
void notify(const std::shared_ptr<ColleagueBase>& sender) const override
{
if (sender == m_componentA) {
std::cout << "Mediator reacts on Component A and triggers following operations:" << std::endl;
m_componentA->operationX();
m_componentB->operationY();
}
else if (sender == m_componentB) {
std::cout << "Mediator reacts on Component B and triggers following operations:" << std::endl;
m_componentA->operationX();
m_componentB->operationY();
m_componentB->operationZ();
}
}
};
static void clientCode()
{
std::shared_ptr<ConcreteColleagueA> c1{ new ConcreteColleagueA() };
std::shared_ptr<ConcreteColleagueB> c2{ new ConcreteColleagueB() };
std::shared_ptr<ConcreteMediator> mediator{
std::make_shared<ConcreteMediator>(c1, c2)
};
// Note: this method call CANNOT be integrated into the c'tor call before:
// the shared pointer object must be fully constructed before 'shared_from_this()'
// may be called without runtime error !!!
mediator->setConcreteColleagues();
std::cout << "Client triggers operation A." << std::endl;
c1->operationA();
std::cout << std::endl;
std::cout << "Client triggers operation D." << std::endl;
c2->operationB();
}
}
void test_conceptual_example02()
{
using namespace ConceptualExample02;
clientCode();
}
// ===========================================================================
// End-of-File
// ===========================================================================