|
1
|
|
|
from abc import ABC, abstractmethod, ABCMeta |
|
2
|
|
|
|
|
3
|
|
|
__all__ = ['GenericMediator', 'BaseComponent'] |
|
4
|
|
|
|
|
5
|
|
|
|
|
6
|
|
|
class Mediator(ABC): |
|
7
|
|
|
""" |
|
8
|
|
|
The Mediator interface declares a method used by components to notify the |
|
9
|
|
|
mediator about various events. The Mediator may react to these events and |
|
10
|
|
|
pass the execution to other components. |
|
11
|
|
|
""" |
|
12
|
|
|
@abstractmethod |
|
13
|
|
|
def notify(self, sender: object, event: str) -> None: |
|
14
|
|
|
"""[summary] |
|
15
|
|
|
|
|
16
|
|
|
Args: |
|
17
|
|
|
sender (object): [description] |
|
18
|
|
|
event (str): [description] |
|
19
|
|
|
""" |
|
20
|
|
|
raise NotImplementedError |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class GenericMediator(Mediator, metaclass=ABCMeta): |
|
24
|
|
|
"""Abstract Mediator class that automatically configures components received as *args through the constructor. |
|
25
|
|
|
""" |
|
26
|
|
|
def __new__(cls, *components, **kwargs): |
|
27
|
|
|
x = super().__new__(cls) |
|
28
|
|
|
for i, component in enumerate(components): |
|
29
|
|
|
setattr(x, f'_component{i+1}', component) |
|
30
|
|
|
getattr(x, f'_component{i+1}').mediator = x |
|
31
|
|
|
return x |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
class BaseComponent: |
|
35
|
|
|
""" |
|
36
|
|
|
The Base Component provides the basic functionality of storing a mediator's |
|
37
|
|
|
instance inside component objects. |
|
38
|
|
|
""" |
|
39
|
|
|
|
|
40
|
|
|
def __init__(self, mediator: Mediator = None) -> None: |
|
41
|
|
|
self._mediator = mediator |
|
42
|
|
|
|
|
43
|
|
|
@property |
|
44
|
|
|
def mediator(self) -> Mediator: |
|
45
|
|
|
return self._mediator |
|
46
|
|
|
|
|
47
|
|
|
@mediator.setter |
|
48
|
|
|
def mediator(self, mediator: Mediator) -> None: |
|
49
|
|
|
self._mediator = mediator |
|
50
|
|
|
|
|
51
|
|
|
|
|
52
|
|
|
if __name__ == "__main__": |
|
53
|
|
|
# The client code. |
|
54
|
|
|
class ConcreteMediator(GenericMediator): |
|
55
|
|
|
def notify(self, sender: object, event: str) -> None: |
|
56
|
|
|
if event == "A": |
|
57
|
|
|
print("Mediator reacts on A and triggers following operations:") |
|
58
|
|
|
self._component2.do_c() |
|
59
|
|
|
elif event == "D": |
|
60
|
|
|
print("Mediator reacts on D and triggers following operations:") |
|
61
|
|
|
self._component1.do_b() |
|
62
|
|
|
self._component2.do_c() |
|
63
|
|
|
|
|
64
|
|
|
""" |
|
65
|
|
|
Concrete Components implement various functionality. They don't depend on other |
|
66
|
|
|
components. They also don't depend on any concrete mediator classes. |
|
67
|
|
|
""" |
|
68
|
|
|
class Component1(BaseComponent): |
|
69
|
|
|
def do_a(self) -> None: |
|
70
|
|
|
print("Component 1 does A.") |
|
71
|
|
|
self.mediator.notify(self, "A") |
|
72
|
|
|
|
|
73
|
|
|
def do_b(self) -> None: |
|
74
|
|
|
print("Component 1 does B.") |
|
75
|
|
|
self.mediator.notify(self, "B") |
|
76
|
|
|
|
|
77
|
|
|
|
|
78
|
|
|
class Component2(BaseComponent): |
|
79
|
|
|
def do_c(self) -> None: |
|
80
|
|
|
print("Component 2 does C.") |
|
81
|
|
|
self.mediator.notify(self, "C") |
|
82
|
|
|
|
|
83
|
|
|
def do_d(self) -> None: |
|
84
|
|
|
print("Component 2 does D.") |
|
85
|
|
|
self.mediator.notify(self, "D") |
|
86
|
|
|
|
|
87
|
|
|
|
|
88
|
|
|
c1 = Component1() |
|
89
|
|
|
c2 = Component2() |
|
90
|
|
|
my_mediator = ConcreteMediator(c1, c2) |
|
91
|
|
|
|
|
92
|
|
|
print("Client triggers operation A.") |
|
93
|
|
|
c1.do_a() |
|
94
|
|
|
|
|
95
|
|
|
print("\n", end="") |
|
96
|
|
|
|
|
97
|
|
|
print("Client triggers operation D.") |
|
98
|
|
|
c2.do_d() |
|
99
|
|
|
|