Mahipal Nehra

3 years ago · 2 min. reading time · 0 ·

Blogging
>
Mahipal blog
>
Overview of Mediator Design Pattern

Overview of Mediator Design Pattern

[TERT JETT [LTT RTT

 

o.
21°
fa
Nr bE]
Q
7)
2 |
° fll
J ||: i
ki
©
Q
2
1:
R=
Re

VA a sasemyog auoz Jaydioaq

Mediator Design Pattern, as the name suggests defines an object that mediates between other objects. It is one of the most important and widely used behavioural design pattern because it promotes loose coupling by refraining objects from communicating with each other explicitly. The mediator object does the task of mediating the information between them. You can find the usage of such a design pattern in applications that involve messaging. If a user wants to broadcast a message to other users, the objects that represent the users, communicate their way through the mediator object that keeps track of all the users and broadcasts it to them. The structure of the application could have been quite messy if there was no mediator, and every user object would have to track down every other user object for communication. The use of mediator design pattern in cases like these reduces dependencies between communicating objects, ultimately reducing coupling. In this article, we are going to define Mediator Design Pattern, mention the key participants that make up this design pattern, explain the use case with an example of mediator design pattern in java and also see when to use mediator design pattern.

What is mediator design pattern?

As mentioned in the introductory paragraph, mediator design pattern’ s intent is to define an object that encapsulates how set of objects interact with each other. It falls in the category of behavioural design pattern. Why? Behavioural Design Patterns describe the patterns of communication between objects, they are concerned with the assignment of responsibilities between objects and mediator design pattern follows the same approach.

Read: “When to Use Composite Design Pattern in Java

In terms of real-world analogy, what can be the significance of a mediator? A traffic signal is a suitable example in this case. It mediates information to vehicles, telling them when to move, stop and wait. If there was no such mediator and every vehicle had to communicate with every other for commutation, you can imagine how dependent everything could have become? That problem is not the case today because there is a mediator (traffic signal) to signal the vehicles when to move and when not to move. Similarly, in the world of objects and in cases like the chat application a mediator design pattern provides a better solution to solve dependency issues.

Read: “All you need to know about Template Method Design Pattern

Another example can be the Document Object Model in web browsers. Whenever we load a web page, the browser creates the Document Object Model (a tree like structure that represents every element of a web page) of that page. The web page is represented by the document object and is considered the entry point to the web page’s content. Any event emission, broadcasting or subscription happens via the document object itself. Instead of binding to the events of the individual nodes, a higher-level object (Document) is given the responsibility of notifying subscribers about interaction events. So, the document object mediates information among other elements (nodes) in a web page. You can think of the document object as the mediator and the DOM can be thought of as the simplest and common real-world glimpse of the mediator design pattern.

Read: “What is Adapter Design Pattern: A brief explanation

Let us see a simple example of the mediator design pattern in java. We are going to use a very abstract example of a messaging service in which two users will send a message to each other. But before that, we need to introduce the key players of the mediator design pattern. They are as follows:

  • Mediator — The mediator defines an interface for communicating between objects.
  • Concrete Mediator — You can think of it as an implementation of the Mediator interface.
  • Colleague Classes — These classes are the ones that communicate information through the mediator.

For the sake of simplicity, our example is going to contain only the concrete mediator and concrete colleague class.

A simple example of mediator design pattern in java (chatroom example)

1. The Mediator (Send Message Mediator)

Fig- mediator design pattern example in java — Mediator Class

Read the full story here: https://www.decipherzone.com/blog-detail/mediator-design-pattern


Comments

Articles from Mahipal Nehra

View blog
3 years ago · 1 min. reading time

Numerous factors determine the destiny of a company including team, creativity, market, product, str ...

3 years ago · 1 min. reading time

Microservice Architecture or Microservices are increasingly being used in the software industry to c ...

2 years ago · 2 min. reading time

Everyone requires a strong relationship with customers to grow their business. But getting more cust ...

You may be interested in these jobs

  • White Hawk Consultant

    Big Data Architect

    Found in: Talent IN 2A C2 - 2 days ago


    White Hawk Consultant Bengaluru, India

    Job Description : · 10+ years of experience in end-to-end software development and system architecture, preferably in a product development organisation. · - Hands-on experience in Java/Scala and Big Data tech stack - Apache Pinot/Druid/Hudi, HDFS, Hive, Presto, Flume, Storm, Kaf ...

  • Hirelo

    SAP MM

    Found in: Talent IN 2A C2 - 2 days ago


    Hirelo Bengaluru, India

    SAP MM & S2P Functional Consultant(6 Months Contract) · About the job : · Mandatory : Work from Office - Monday to friday, good availability on weekends. · - Should have good hands-on Experience · - Should have at least 5 years of experience in a full cycle implementation as wel ...

  • Indus Group Inc

    Bench Sales Recriuter

    Found in: Talent IN C2 - 2 days ago


    Indus Group Inc Hyderabad, India Full time

    Company Overview · Indus Group Inc is a leading Software Development and Consulting Company specializing in providing high-quality technology solutions to our clients. With a strong focus on client success, we work closely with management and decision-makers to deliver top-notch ...