Usage examples: The most popular usage of the Mediator pattern in Python code is facilitating communications between GUI components of an app. The synonym of the Mediator is the Controller part of MVC pattern.
Conceptual Example
This example illustrates the structure of the Mediator design pattern. It focuses on answering these questions:
What classes does it consist of?
What roles do these classes play?
In what way the elements of the pattern are related?
main.py: Conceptual example
from__future__import annotationsfrom abc import ABCclassMediator(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. """defnotify(self,sender:object,event:str) ->None:passclassConcreteMediator(Mediator):def__init__(self,component1: Component1,component2: Component2) ->None: self._component1 = component1 self._component1.mediator = self self._component2 = component2 self._component2.mediator = selfdefnotify(self,sender:object,event:str) ->None:if event =="A":print("Mediator reacts on A and triggers following operations:") self._component2.do_c()elif event =="D":print("Mediator reacts on D and triggers following operations:") self._component1.do_b() self._component2.do_c()classBaseComponent:""" 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@propertydefmediator(self) -> Mediator:return self._mediator@mediator.setterdefmediator(self,mediator: Mediator) ->None: self._mediator = mediator"""Concrete Components implement various functionality. They don't depend on othercomponents. They also don't depend on any concrete mediator classes."""classComponent1(BaseComponent):defdo_a(self) ->None:print("Component 1 does A.") self.mediator.notify(self, "A")defdo_b(self) ->None:print("Component 1 does B.") self.mediator.notify(self, "B")classComponent2(BaseComponent):defdo_c(self) ->None:print("Component 2 does C.") self.mediator.notify(self, "C")defdo_d(self) ->None:print("Component 2 does D.") self.mediator.notify(self, "D")if__name__=="__main__":# The client code. c1 =Component1() c2 =Component2() mediator =ConcreteMediator(c1, c2)print("Client triggers operation A.") c1.do_a()print("\n", end="")print("Client triggers operation D.") c2.do_d()
Output.txt: Execution result
Client triggers operation A.
Component 1 does A.
Mediator reacts on A and triggers following operations:
Component 2 does C.
Client triggers operation D.
Component 2 does D.
Mediator reacts on D and triggers following operations:
Component 1 does B.
Component 2 does C.