Conditions | 3 |
Total Lines | 47 |
Code Lines | 33 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
1 | |||
2 | def test_mediator_scenario(): |
||
3 | from so_magic.utils import GenericMediator, BaseComponent |
||
4 | # The client code. |
||
5 | class ConcreteMediator(GenericMediator): |
||
6 | def notify(self, sender: object, event: str) -> None: |
||
7 | if event == "A": |
||
8 | print("Mediator reacts on A and triggers following operations:") |
||
9 | self._component2.do_c() |
||
10 | elif event == "D": |
||
11 | print("Mediator reacts on D and triggers following operations:") |
||
12 | self._component1.do_b() |
||
13 | self._component2.do_c() |
||
14 | |||
15 | """ |
||
16 | Concrete Components implement various functionality. They don't depend on other |
||
17 | components. They also don't depend on any concrete mediator classes. |
||
18 | """ |
||
19 | |||
20 | class Component1(BaseComponent): |
||
21 | def do_a(self) -> None: |
||
22 | print("Component 1 does A.") |
||
23 | self.mediator.notify(self, "A") |
||
24 | |||
25 | def do_b(self) -> None: |
||
26 | print("Component 1 does B.") |
||
27 | self.mediator.notify(self, "B") |
||
28 | |||
29 | class Component2(BaseComponent): |
||
30 | def do_c(self) -> None: |
||
31 | print("Component 2 does C.") |
||
32 | self.mediator.notify(self, "C") |
||
33 | |||
34 | def do_d(self) -> None: |
||
35 | print("Component 2 does D.") |
||
36 | self.mediator.notify(self, "D") |
||
37 | |||
38 | c1 = Component1() |
||
39 | c2 = Component2() |
||
40 | my_mediator = ConcreteMediator(c1, c2) |
||
41 | |||
42 | print("Client triggers operation A.") |
||
43 | c1.do_a() |
||
44 | |||
45 | print("\n", end="") |
||
46 | |||
47 | print("Client triggers operation D.") |
||
48 | c2.do_d() |
||
49 |