|
| 1 | + |
| 2 | +#include <string> |
| 3 | +#include <iostream> |
| 4 | +/** |
| 5 | + * EN: Mediator Design Pattern |
| 6 | + * |
| 7 | + * Intent: Lets you reduce chaotic dependencies between objects. The pattern |
| 8 | + * restricts direct communications between the objects and forces them to |
| 9 | + * collaborate only via a mediator object. |
| 10 | + * |
| 11 | + * RU: Паттерн Посредник |
| 12 | + * |
| 13 | + * Назначение: Позволяет уменьшить связанность множества классов между собой, |
| 14 | + * благодаря перемещению этих связей в один класс-посредник. |
| 15 | + */ |
| 16 | + |
| 17 | +/** |
| 18 | + * EN: The Mediator interface declares a method used by components to notify the |
| 19 | + * mediator about various events. The Mediator may react to these events and |
| 20 | + * pass the execution to other components. |
| 21 | + * |
| 22 | + * RU: Интерфейс Посредника предоставляет метод, используемый компонентами для |
| 23 | + * уведомления посредника о различных событиях. Посредник может реагировать на |
| 24 | + * эти события и передавать исполнение другим компонентам. |
| 25 | + */ |
| 26 | +class BaseComponent; |
| 27 | +class Mediator |
| 28 | +{ |
| 29 | +public: |
| 30 | + virtual void Notify(BaseComponent *sender, std::string event) const = 0; |
| 31 | +}; |
| 32 | + |
| 33 | +/** |
| 34 | + * EN: The Base Component provides the basic functionality of storing a |
| 35 | + * mediator's instance inside component objects. |
| 36 | + * |
| 37 | + * RU: Базовый Компонент обеспечивает базовую функциональность хранения |
| 38 | + * экземпляра посредника внутри объектов компонентов. |
| 39 | + */ |
| 40 | +class BaseComponent |
| 41 | +{ |
| 42 | +protected: |
| 43 | + Mediator *mediator_; |
| 44 | + |
| 45 | +public: |
| 46 | + BaseComponent(Mediator *mediator = nullptr) : mediator_(mediator) |
| 47 | + { |
| 48 | + } |
| 49 | + void set_mediator(Mediator *mediator) |
| 50 | + { |
| 51 | + this->mediator_ = mediator; |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +/** |
| 56 | + * EN: Concrete Components implement various functionality. They don't depend on |
| 57 | + * other components. They also don't depend on any concrete mediator classes. |
| 58 | + * |
| 59 | + * RU: Конкретные Компоненты реализуют различную функциональность. Они не |
| 60 | + * зависят от других компонентов. Они также не зависят от каких-либо конкретных |
| 61 | + * классов посредников. |
| 62 | + */ |
| 63 | +class Component1 : public BaseComponent |
| 64 | +{ |
| 65 | +public: |
| 66 | + void DoA() |
| 67 | + { |
| 68 | + std::cout << "Component 1 does A.\n"; |
| 69 | + this->mediator_->Notify(this, "A"); |
| 70 | + } |
| 71 | + void DoB() |
| 72 | + { |
| 73 | + std::cout << "Component 1 does B.\n"; |
| 74 | + this->mediator_->Notify(this, "B"); |
| 75 | + } |
| 76 | +}; |
| 77 | + |
| 78 | +class Component2 : public BaseComponent |
| 79 | +{ |
| 80 | +public: |
| 81 | + void DoC() |
| 82 | + { |
| 83 | + std::cout << "Component 2 does C.\n"; |
| 84 | + this->mediator_->Notify(this, "C"); |
| 85 | + } |
| 86 | + void DoD() |
| 87 | + { |
| 88 | + std::cout << "Component 2 does D.\n"; |
| 89 | + this->mediator_->Notify(this, "D"); |
| 90 | + } |
| 91 | +}; |
| 92 | + |
| 93 | +/** |
| 94 | + * EN: Concrete Mediators implement cooperative behavior by coordinating several |
| 95 | + * components. |
| 96 | + * |
| 97 | + * RU: Конкретные Посредники реализуют совместное поведение, координируя |
| 98 | + * отдельные компоненты. |
| 99 | + */ |
| 100 | +class ConcreteMediator : public Mediator |
| 101 | +{ |
| 102 | +private: |
| 103 | + Component1 *component1_; |
| 104 | + Component2 *component2_; |
| 105 | + |
| 106 | +public: |
| 107 | + ConcreteMediator(Component1 *c1, Component2 *c2) : component1_(c1), component2_(c2) |
| 108 | + { |
| 109 | + |
| 110 | + this->component1_->set_mediator(this); |
| 111 | + this->component2_->set_mediator(this); |
| 112 | + } |
| 113 | + void Notify(BaseComponent *sender, std::string event) const override |
| 114 | + { |
| 115 | + if (event == "A") |
| 116 | + { |
| 117 | + std::cout << "Mediator reacts on A and triggers following operations:\n"; |
| 118 | + this->component2_->DoC(); |
| 119 | + } |
| 120 | + if (event == "D") |
| 121 | + { |
| 122 | + std::cout << "Mediator reacts on D and triggers following operations:\n"; |
| 123 | + this->component1_->DoB(); |
| 124 | + this->component2_->DoC(); |
| 125 | + } |
| 126 | + } |
| 127 | +}; |
| 128 | + |
| 129 | +/** |
| 130 | + * EN: The client code. |
| 131 | + * |
| 132 | + * RU: Клиентский код. |
| 133 | + */ |
| 134 | + |
| 135 | +void ClientCode() |
| 136 | +{ |
| 137 | + Component1 *c1 = new Component1; |
| 138 | + Component2 *c2 = new Component2; |
| 139 | + ConcreteMediator *mediator = new ConcreteMediator(c1, c2); |
| 140 | + std::cout << "Client triggers operation A.\n"; |
| 141 | + c1->DoA(); |
| 142 | + std::cout << "\n"; |
| 143 | + std::cout << "Client triggers operation D.\n"; |
| 144 | + c2->DoD(); |
| 145 | + |
| 146 | + delete c1; |
| 147 | + delete c2; |
| 148 | + delete mediator; |
| 149 | +} |
| 150 | + |
| 151 | +int main() |
| 152 | +{ |
| 153 | + ClientCode(); |
| 154 | + return 0; |
| 155 | +} |
0 commit comments