add code example

This commit is contained in:
Ruidy 2020-10-02 17:22:49 +02:00
parent 9e8fba159b
commit 14a480cc5c
7 changed files with 92 additions and 2 deletions

View file

@ -44,7 +44,7 @@ and divided into three groups.
- [Iterator](behavioral/iterator/README.md)
- [Mediator](behavioral/mediator/README.md)
## Ressources
## Resources
- [Refactoring Guru](https://refactoring.guru/design-patterns/catalog)
- [Python Design Patterns](https://python-patterns.guide/)

View file

@ -1,5 +1,5 @@
"""
lets you reduce chaotic dependencies between objects. The pattern restricts
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.
"""

View file

@ -0,0 +1,19 @@
from behavioral.mediator.mediator import Mediator
class BaseComponent:
"""
The Base Component provides the basic functionality of storing a mediator's
instance inside component objects.
"""
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

View file

@ -0,0 +1,25 @@
"""
Concrete Components implement various functionality. They don't depend on other
components. They also don't depend on any concrete mediator classes.
"""
from behavioral.mediator.base_component import BaseComponent
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")

View file

@ -0,0 +1,20 @@
from behavioral.mediator.components import Component2, Component1
from behavioral.mediator.mediator import Mediator
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()
if event == "D":
print("Mediator reacts on D and triggers following operations:")
self._component1.do_b()
self._component2.do_c()

View file

@ -0,0 +1,14 @@
"""The client code."""
from behavioral.mediator.components import Component1, Component2
from behavioral.mediator.concrete_mediator import ConcreteMediator
c1 = Component1()
c2 = Component2()
mediator = ConcreteMediator(c1, c2)
print("Client triggers operation A.")
c1.do_a()
print("")
print("Client triggers operation D.")
c2.do_d()

View file

@ -0,0 +1,12 @@
from abc import ABC
class Mediator(ABC):
"""
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.
"""
def notify(self, sender: object, event: str) -> None:
pass