|
1 |
| -using System; |
| 1 | +// EN: Mediator Design Pattern |
| 2 | +// |
| 3 | +// Intent: Define an object that encapsulates how a set of objects interact. |
| 4 | +// Mediator promotes loose coupling by keeping objects from referring to each |
| 5 | +// other explicitly, and it lets you vary their interaction independently. |
| 6 | +// |
| 7 | +// RU: Паттерн Посредник |
| 8 | +// |
| 9 | +// Назначение: Определяет объект, который инкапсулирует взаимодействие набора |
| 10 | +// объектов. Посредник способствует слабой связанности, удерживая объекты от |
| 11 | +// обращения друг к другу напрямую, и это позволяет вам менять их взаимодействие |
| 12 | +// независимо. |
| 13 | + |
| 14 | +using System; |
2 | 15 |
|
3 | 16 | namespace RefactoringGuru.DesignPatterns.Mediator.Conceptual
|
4 | 17 | {
|
| 18 | + // EN: The Mediator interface declares a method used by components to notify the |
| 19 | + // mediator about various eventents. The Mediator may react to these eventents and |
| 20 | + // pass the execution to other components. |
| 21 | + // |
| 22 | + // RU: Интерфейс Посредника предоставляет метод, используемый компонентами для |
| 23 | + // уведомления посредника о различных событиях. Посредник может реагировать на |
| 24 | + // эти события и передавать исполнение другим компонентам. |
5 | 25 | interface Mediator
|
6 | 26 | {
|
7 |
| - void notify(object sender, string ev); |
| 27 | + void Notify(object sender, string ev); |
8 | 28 | }
|
9 | 29 |
|
| 30 | + // EN: Concrete Mediators implement cooperative behavior by coordinating several |
| 31 | + // components. |
| 32 | + // |
| 33 | + // RU: Конкретные Посредники реализуют совместное поведение, координируя |
| 34 | + // отдельные компоненты. |
10 | 35 | class ConcreteMediator : Mediator
|
11 | 36 | {
|
12 |
| - private Component1 component1; |
| 37 | + private Component1 _component1; |
13 | 38 |
|
14 |
| - private Component2 component2; |
| 39 | + private Component2 _component2; |
15 | 40 |
|
16 | 41 | public ConcreteMediator(Component1 component1, Component2 component2)
|
17 | 42 | {
|
18 |
| - this.component1 = component1; |
19 |
| - this.component1.setMediator(this); |
20 |
| - this.component2 = component2; |
21 |
| - this.component2.setMediator(this); |
| 43 | + this._component1 = component1; |
| 44 | + this._component1.SetMediator(this); |
| 45 | + this._component2 = component2; |
| 46 | + this._component2.SetMediator(this); |
22 | 47 | }
|
23 | 48 |
|
24 |
| - public void notify(object sender, string ev) |
| 49 | + public void Notify(object sender, string ev) |
25 | 50 | {
|
26 | 51 | if (ev == "A")
|
27 | 52 | {
|
28 |
| - Console.Write("Mediator reacts on A and triggers folowing operations:\n"); |
29 |
| - this.component2.doC(); |
| 53 | + Console.WriteLine("Mediator reacts on A and triggers folowing operations:"); |
| 54 | + this._component2.DoC(); |
30 | 55 | }
|
31 | 56 | if (ev == "D")
|
32 | 57 | {
|
33 |
| - Console.Write("Mediator reacts on D and triggers following operations:\n"); |
34 |
| - this.component1.doB(); |
35 |
| - this.component2.doC(); |
| 58 | + Console.WriteLine("Mediator reacts on D and triggers following operations:"); |
| 59 | + this._component1.DoB(); |
| 60 | + this._component2.DoC(); |
36 | 61 | }
|
37 | 62 | }
|
38 | 63 | }
|
39 | 64 |
|
| 65 | + // EN: The Base Component provides the basic functionality of storing a |
| 66 | + // mediator's instance inside component objects. |
| 67 | + // |
| 68 | + // RU: Базовый Компонент обеспечивает базовую функциональность хранения |
| 69 | + // экземпляра посредника внутри объектов компонентов. |
40 | 70 | class BaseComponent
|
41 | 71 | {
|
42 |
| - protected Mediator mediator; |
| 72 | + protected Mediator _mediator; |
43 | 73 |
|
44 | 74 | public BaseComponent(Mediator mediator = null)
|
45 | 75 | {
|
46 |
| - this.mediator = mediator; |
| 76 | + this._mediator = mediator; |
47 | 77 | }
|
48 | 78 |
|
49 |
| - public void setMediator(Mediator mediator) |
| 79 | + public void SetMediator(Mediator mediator) |
50 | 80 | {
|
51 |
| - this.mediator = mediator; |
| 81 | + this._mediator = mediator; |
52 | 82 | }
|
53 | 83 | }
|
54 | 84 |
|
| 85 | + // EN: Concrete Components implement various functionality. They don't depend on |
| 86 | + // other components. They also don't depend on any concrete mediator classes. |
| 87 | + // |
| 88 | + // RU: Конкретные Компоненты реализуют различную функциональность. Они не |
| 89 | + // зависят от других компонентов. Они также не зависят от каких-либо конкретных |
| 90 | + // классов посредников. |
55 | 91 | class Component1 : BaseComponent
|
56 | 92 | {
|
57 |
| - public void doA() |
| 93 | + public void DoA() |
58 | 94 | {
|
59 |
| - Console.Write("Component 1 does A.\n"); |
| 95 | + Console.WriteLine("Component 1 does A."); |
60 | 96 |
|
61 |
| - this.mediator.notify(this, "A"); |
| 97 | + this._mediator.Notify(this, "A"); |
62 | 98 | }
|
63 | 99 |
|
64 |
| - public void doB() |
| 100 | + public void DoB() |
65 | 101 | {
|
66 |
| - Console.Write("Component 1 does B.\n"); |
| 102 | + Console.WriteLine("Component 1 does B."); |
67 | 103 |
|
68 |
| - this.mediator.notify(this, "B"); |
| 104 | + this._mediator.Notify(this, "B"); |
69 | 105 | }
|
70 | 106 | }
|
71 | 107 |
|
72 | 108 | class Component2 : BaseComponent
|
73 | 109 | {
|
74 |
| - public void doC() |
| 110 | + public void DoC() |
75 | 111 | {
|
76 |
| - Console.Write("Component 2 does C.\n"); |
| 112 | + Console.WriteLine("Component 2 does C."); |
77 | 113 |
|
78 |
| - this.mediator.notify(this, "C"); |
| 114 | + this._mediator.Notify(this, "C"); |
79 | 115 | }
|
80 | 116 |
|
81 |
| - public void doD() |
| 117 | + public void DoD() |
82 | 118 | {
|
83 |
| - Console.Write("Component 2 does D.\n"); |
| 119 | + Console.WriteLine("Component 2 does D."); |
84 | 120 |
|
85 |
| - this.mediator.notify(this, "D"); |
86 |
| - } |
87 |
| - } |
88 |
| - |
89 |
| - class Program |
90 |
| - { |
91 |
| - static void Main(string[] args) |
92 |
| - { |
93 |
| - Client.ClientCode(); |
| 121 | + this._mediator.Notify(this, "D"); |
94 | 122 | }
|
95 | 123 | }
|
96 | 124 |
|
97 | 125 | class Client
|
98 | 126 | {
|
99 | 127 | public static void ClientCode()
|
100 | 128 | {
|
| 129 | + // EN: The client code. |
| 130 | + // |
| 131 | + // RU: Клиентский код. |
101 | 132 | Component1 component1 = new Component1();
|
102 | 133 | Component2 component2 = new Component2();
|
103 |
| - Mediator mediator = new ConcreteMediator(component1, component2); |
| 134 | + new ConcreteMediator(component1, component2); |
104 | 135 |
|
105 | 136 | Console.Write("Client triggets operation A.\n");
|
106 |
| - component1.doA(); |
| 137 | + component1.DoA(); |
107 | 138 |
|
108 | 139 | Console.WriteLine();
|
109 | 140 |
|
110 | 141 | Console.Write("Client triggers operation D.\n");
|
111 |
| - component2.doD(); |
| 142 | + component2.DoD(); |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + class Program |
| 147 | + { |
| 148 | + static void Main(string[] args) |
| 149 | + { |
| 150 | + Client.ClientCode(); |
112 | 151 | }
|
113 | 152 | }
|
114 | 153 | }
|
0 commit comments