Skip to content

Commit e2da5e2

Browse files
committed
some work on mediator
1 parent 889fa17 commit e2da5e2

File tree

4 files changed

+55
-84
lines changed

4 files changed

+55
-84
lines changed

mediator/README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Mediator Design Pattern
2+
3+
In software engineering, the mediator pattern defines an object that encapsulates how a set of objects interact. This pattern is considered to be a behavioral pattern due to the way it can alter the program's running behavior.
4+
5+
With the mediator pattern, communication between objects is encapsulated within a mediator object. Objects no longer communicate directly with each other, but instead communicate through the mediator. This reduces the dependencies between communicating objects, thereby reducing coupling.
6+
7+
The Mediator interface declares a method used by components to notify the
8+
mediator about various events. The Mediator may react to these events and
9+
pass the execution to other components.
10+
11+
The Base Component provides the basic functionality of storing a mediator's
12+
instance inside component objects.
13+
14+
Concrete Components implement various functionality. They don't depend on other
15+
components. They also don't depend on any concrete mediator classes.

mediator/README.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

mediator/classes.dot

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
digraph "classes" {
22
charset="utf-8"
33
rankdir=BT
4-
"0" [label="{BaseComponent|mediator\l|}", shape="record"];
5-
"1" [label="{Component1|\l|do_a()\ldo_b()\l}", shape="record"];
6-
"2" [label="{Component2|\l|do_c()\ldo_d()\l}", shape="record"];
7-
"3" [label="{ConcreteMediator|\l|notify()\l}", shape="record"];
8-
"4" [label="{Mediator|\l|notify()\l}", shape="record"];
9-
"1" -> "0" [arrowhead="empty", arrowtail="none"];
10-
"2" -> "0" [arrowhead="empty", arrowtail="none"];
11-
"3" -> "4" [arrowhead="empty", arrowtail="none"];
4+
"0" [label="{Component|mediator\lname\l|notify()\lreceive()\l}", shape="record"];
5+
"1" [label="{IComponent|\l|notify()\lreceive()\l}", shape="record"];
6+
"2" [label="{Mediator|components : list\l|add()\lnotify()\l}", shape="record"];
7+
"0" -> "1" [arrowhead="empty", arrowtail="none"];
128
}

mediator/mediator.py

Lines changed: 36 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,50 @@
1-
from __future__ import annotations
2-
from abc import ABC
1+
from abc import ABCMeta, abstractmethod
32

43

5-
class Mediator(ABC):
6-
"""
7-
The Mediator interface declares a method used by components to notify the
8-
mediator about various events. The Mediator may react to these events and
9-
pass the execution to other components.
10-
"""
4+
class IComponent(metaclass=ABCMeta):
5+
@staticmethod
6+
@abstractmethod
7+
def notify(msg):
8+
"""The required notify method"""
119

12-
def notify(self, sender: object, event: str) -> None:
13-
pass
10+
@staticmethod
11+
@abstractmethod
12+
def receive(msg):
13+
"""The required receive method"""
1414

1515

16-
class ConcreteMediator(Mediator):
17-
def __init__(self, component1: Component1, component2: Component2) -> None:
18-
self._component1 = component1
19-
self._component1.mediator = self
20-
self._component2 = component2
21-
self._component2.mediator = self
16+
class Component(IComponent):
17+
def __init__(self, mediator, name):
18+
self.mediator = mediator
19+
self.name = name
2220

23-
def notify(self, sender: object, event: str) -> None:
24-
if event == "A":
25-
print("Mediator reacts on A and triggers following operations:")
26-
self._component2.do_c()
27-
elif event == "D":
28-
print("Mediator reacts on D and triggers following operations:")
29-
self._component1.do_b()
30-
self._component2.do_c()
21+
def notify(self, message):
22+
print(self.name + ": Sending : " + message)
23+
self.mediator.notify(message, self)
3124

25+
def receive(self, message):
26+
print(self.name + ": Received : " + message)
3227

33-
class BaseComponent:
34-
"""
35-
The Base Component provides the basic functionality of storing a mediator's
36-
instance inside component objects.
37-
"""
3828

39-
def __init__(self, mediator: Mediator = None) -> None:
40-
self._mediator = mediator
29+
class Mediator:
30+
def __init__(self):
31+
self.components = []
4132

42-
@property
43-
def mediator(self) -> Mediator:
44-
return self._mediator
33+
def add(self, component):
34+
self.components.append(component)
4535

46-
@mediator.setter
47-
def mediator(self, mediator: Mediator) -> None:
48-
self._mediator = mediator
36+
def notify(self, message, component):
37+
for _component in self.components:
38+
if _component != component:
39+
_component.receive(message)
4940

5041

51-
"""
52-
Concrete Components implement various functionality. They don't depend on other
53-
components. They also don't depend on any concrete mediator classes.
54-
"""
42+
MEDIATOR = Mediator()
43+
COMPONENT1 = Component(MEDIATOR, "Component1")
44+
COMPONENT2 = Component(MEDIATOR, "Component2")
45+
COMPONENT3 = Component(MEDIATOR, "Component3")
46+
MEDIATOR.add(COMPONENT1)
47+
MEDIATOR.add(COMPONENT2)
48+
MEDIATOR.add(COMPONENT3)
5549

56-
57-
class Component1(BaseComponent):
58-
def do_a(self) -> None:
59-
print("Component 1 does A.")
60-
self.mediator.notify(self, "A")
61-
62-
def do_b(self) -> None:
63-
print("Component 1 does B.")
64-
self.mediator.notify(self, "B")
65-
66-
67-
class Component2(BaseComponent):
68-
def do_c(self) -> None:
69-
print("Component 2 does C.")
70-
self.mediator.notify(self, "C")
71-
72-
def do_d(self) -> None:
73-
print("Component 2 does D.")
74-
self.mediator.notify(self, "D")
75-
76-
77-
if __name__ == "__main__":
78-
# The client code.
79-
c1 = Component1()
80-
c2 = Component2()
81-
mediator = ConcreteMediator(c1, c2)
82-
83-
print("Client triggers operation A.")
84-
c1.do_a()
85-
86-
print("\n", end="")
87-
88-
print("Client triggers operation D.")
89-
c2.do_d()
50+
COMPONENT1.notify("Lets Play")

0 commit comments

Comments
 (0)