-
Notifications
You must be signed in to change notification settings - Fork 0
/
Mediator.cpp
117 lines (87 loc) Β· 2.59 KB
/
Mediator.cpp
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
/*
* Mediator pattern
*
* Intent: encapsulates dependencies between objects. The pattern forces objects
* to communicate via mediator.
*/
#include <iostream>
#include <memory>
enum class Events { A, B };
class Component;
class Mediator {
public:
virtual ~Mediator() = default;
virtual void notify(Component& sender, Events event) const = 0;
};
class Component {
public:
Component(Mediator* mediator = nullptr) : _mediator(mediator) {}
void set_mediator(Mediator* mediator) {
_mediator.reset(mediator);
}
protected:
std::unique_ptr<Mediator> _mediator;
};
// Component don't depend on other components. They also don't depend on concrete mediator class.
class ConcreteComponentA : public Component {
public:
void do_logic() const {
std::cout << "Concrete component A does some logic..." << '\n';
}
void do_b() {
_mediator->notify(*this, Events::B);
}
};
class ConcreteComponentB : public Component {
public:
void do_logic() const {
std::cout << "Concrete component B does some logic..." << '\n';
}
void do_a() {
_mediator->notify(*this, Events::A);
}
};
// Concrete Mediator implement concrete behavior by coordinating components
class ConcreteMediator : public Mediator {
public:
ConcreteMediator(ConcreteComponentA& component_a, ConcreteComponentB& component_b)
: _component_a(component_a),
_component_b(component_b) {
component_a.set_mediator(this);
component_b.set_mediator(this);
}
void notify(Component& sender, Events event) const override {
if (event == Events::A) {
std::cout << "Mediator reacts on A and triggers component A:" << '\n';
_component_a.do_logic();
}
else if (event == Events::B) {
std::cout << "Mediator reacts on B and triggers component B:" << '\n';
_component_b.do_logic();
}
else {
std::cout << "Operation type undefined..." << '\n';
}
std::cout << '\n';
}
private:
ConcreteComponentA& _component_a;
ConcreteComponentB& _component_b;
};
void client() {
ConcreteComponentA component_a{};
ConcreteComponentB component_b{};
ConcreteMediator mediator{component_a, component_b};
component_a.do_b();
// Output:
// Mediator reacts on B and triggers component B:
// Concrete component B does some logic...
component_b.do_a();
// Output:
// Mediator reacts on A and triggers component A:
// Concrete component A does some logic...
}
int main() {
client();
return 0;
}