Author - StudySection Post Views - 193 views
Python

Mediator pattern with an example in Python

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

  1. Easier to maintain as a single object is to refer to communication between all classes.
  2. Introducing new mediators is not disturbing old code.
  3. It increases the code reusability as components of the mediator class follow inheritance.

DISADVANTAGES

  1. Using a mediator centralizes the control of communication between classes.
  2. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

fiteesports.com rivierarw.com cratosroyalbet betwoon grandpashabet grandpashabet giriş deneme bonusu veren siteler casino siteleri