INTRODUCTION
The mediator is a Behavioral Design Pattern adopted to reduce communication complexity between multiple objects. A mediator object is used for communication between classes instead of direct communication which promotes loose coupling between classes and makes the program more extendable.
EXAMPLE
In this example, class “Mediator” implements communication by knowing and maintaining other classes’ objects. Class A and B communicate through mediator objects with each other.
class Mediator:
def __init__(self):
self._A = A(self)
self._B = B(self)
class A:
def __init__(self, mediator):
self._mediator = mediator
class B:
def __init__(self, mediator):
self._mediator = mediator
def main():
mediator = Mediator()
if __name__ == "__main__":
main()
ADVANTAGES
- Easier to maintain as a single object is to refer to communication between all classes.
- Introducing new mediators is not disturbing old code.
- It increases the code reusability as components of the mediator class follow inheritance.
DISADVANTAGES
- Using a mediator centralizes the control of communication between classes.
- Sometimes meditator’s object becomes complex if we write too much logic in it.
StudySection gives an opportunity to beginners and experts in .NET framework to go through StudySection’s .NET Certification Exam and get a .NET certification for enhancement of career in programming. If you have knowledge of the .NET framework then you can get a certificate through an online exam at StudySection.