test_mediator   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 34
dl 0
loc 49
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A test_mediator_scenario() 0 47 3
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