Skip to content

Commit 76152d3

Browse files
committed
Mediator: improve conceptual example
1 parent 5fa01ea commit 76152d3

File tree

1 file changed

+80
-41
lines changed

1 file changed

+80
-41
lines changed

Mediator.Conceptual/Program.cs

Lines changed: 80 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,153 @@
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;
215

316
namespace RefactoringGuru.DesignPatterns.Mediator.Conceptual
417
{
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+
// эти события и передавать исполнение другим компонентам.
525
interface Mediator
626
{
7-
void notify(object sender, string ev);
27+
void Notify(object sender, string ev);
828
}
929

30+
// EN: Concrete Mediators implement cooperative behavior by coordinating several
31+
// components.
32+
//
33+
// RU: Конкретные Посредники реализуют совместное поведение, координируя
34+
// отдельные компоненты.
1035
class ConcreteMediator : Mediator
1136
{
12-
private Component1 component1;
37+
private Component1 _component1;
1338

14-
private Component2 component2;
39+
private Component2 _component2;
1540

1641
public ConcreteMediator(Component1 component1, Component2 component2)
1742
{
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);
2247
}
2348

24-
public void notify(object sender, string ev)
49+
public void Notify(object sender, string ev)
2550
{
2651
if (ev == "A")
2752
{
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();
3055
}
3156
if (ev == "D")
3257
{
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();
3661
}
3762
}
3863
}
3964

65+
// EN: The Base Component provides the basic functionality of storing a
66+
// mediator's instance inside component objects.
67+
//
68+
// RU: Базовый Компонент обеспечивает базовую функциональность хранения
69+
// экземпляра посредника внутри объектов компонентов.
4070
class BaseComponent
4171
{
42-
protected Mediator mediator;
72+
protected Mediator _mediator;
4373

4474
public BaseComponent(Mediator mediator = null)
4575
{
46-
this.mediator = mediator;
76+
this._mediator = mediator;
4777
}
4878

49-
public void setMediator(Mediator mediator)
79+
public void SetMediator(Mediator mediator)
5080
{
51-
this.mediator = mediator;
81+
this._mediator = mediator;
5282
}
5383
}
5484

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+
// классов посредников.
5591
class Component1 : BaseComponent
5692
{
57-
public void doA()
93+
public void DoA()
5894
{
59-
Console.Write("Component 1 does A.\n");
95+
Console.WriteLine("Component 1 does A.");
6096

61-
this.mediator.notify(this, "A");
97+
this._mediator.Notify(this, "A");
6298
}
6399

64-
public void doB()
100+
public void DoB()
65101
{
66-
Console.Write("Component 1 does B.\n");
102+
Console.WriteLine("Component 1 does B.");
67103

68-
this.mediator.notify(this, "B");
104+
this._mediator.Notify(this, "B");
69105
}
70106
}
71107

72108
class Component2 : BaseComponent
73109
{
74-
public void doC()
110+
public void DoC()
75111
{
76-
Console.Write("Component 2 does C.\n");
112+
Console.WriteLine("Component 2 does C.");
77113

78-
this.mediator.notify(this, "C");
114+
this._mediator.Notify(this, "C");
79115
}
80116

81-
public void doD()
117+
public void DoD()
82118
{
83-
Console.Write("Component 2 does D.\n");
119+
Console.WriteLine("Component 2 does D.");
84120

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");
94122
}
95123
}
96124

97125
class Client
98126
{
99127
public static void ClientCode()
100128
{
129+
// EN: The client code.
130+
//
131+
// RU: Клиентский код.
101132
Component1 component1 = new Component1();
102133
Component2 component2 = new Component2();
103-
Mediator mediator = new ConcreteMediator(component1, component2);
134+
new ConcreteMediator(component1, component2);
104135

105136
Console.Write("Client triggets operation A.\n");
106-
component1.doA();
137+
component1.DoA();
107138

108139
Console.WriteLine();
109140

110141
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();
112151
}
113152
}
114153
}

0 commit comments

Comments
 (0)