forked from RefactoringGuru/design-patterns-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
116 lines (84 loc) · 3.8 KB
/
main.py
File metadata and controls
116 lines (84 loc) · 3.8 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
"""
EN: Mediator Design Pattern
Intent: Lets you reduce chaotic dependencies between objects. The pattern
restricts direct communications between the objects and forces them to
collaborate only via a mediator object.
RU: Паттерн Посредник
Назначение: Позволяет уменьшить связанность множества классов между собой,
благодаря перемещению этих связей в один класс-посредник.
"""
from __future__ import annotations
from abc import ABC
class Mediator(ABC):
"""
EN: 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.
RU: Интерфейс Посредника предоставляет метод, используемый компонентами для
уведомления посредника о различных событиях. Посредник может реагировать на
эти события и передавать исполнение другим компонентам.
"""
def notify(self, sender: object, event: str) -> None:
pass
class ConcreteMediator(Mediator):
def __init__(self, component1: Component1, component2: Component2) -> None:
self._component1 = component1
self._component1.mediator = self
self._component2 = component2
self._component2.mediator = self
def notify(self, sender: object, event: str) -> None:
if event == "A":
print("Mediator reacts on A and triggers following operations:")
self._component2.do_c()
elif event == "D":
print("Mediator reacts on D and triggers following operations:")
self._component1.do_b()
self._component2.do_c()
class BaseComponent:
"""
EN: The Base Component provides the basic functionality of storing a
mediator's instance inside component objects.
RU: Базовый Компонент обеспечивает базовую функциональность хранения
экземпляра посредника внутри объектов компонентов.
"""
def __init__(self, mediator: Mediator = None) -> None:
self._mediator = mediator
@property
def mediator(self) -> Mediator:
return self._mediator
@mediator.setter
def mediator(self, mediator: Mediator) -> None:
self._mediator = mediator
"""
EN: Concrete Components implement various functionality. They don't depend on
other components. They also don't depend on any concrete mediator classes.
RU: Конкретные Компоненты реализуют различную функциональность. Они не зависят
от других компонентов. Они также не зависят от каких-либо конкретных классов
посредников.
"""
class Component1(BaseComponent):
def do_a(self) -> None:
print("Component 1 does A.")
self.mediator.notify(self, "A")
def do_b(self) -> None:
print("Component 1 does B.")
self.mediator.notify(self, "B")
class Component2(BaseComponent):
def do_c(self) -> None:
print("Component 2 does C.")
self.mediator.notify(self, "C")
def do_d(self) -> None:
print("Component 2 does D.")
self.mediator.notify(self, "D")
if __name__ == "__main__":
# EN: The client code.
#
# RU: Клиентский код.
c1 = Component1()
c2 = Component2()
mediator = ConcreteMediator(c1, c2)
print("Client triggers operation A.")
c1.do_a()
print("\n", end="")
print("Client triggers operation D.")
c2.do_d()