Mahipal Nehra

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

Contact the author

Like Share Comment
Mahipal blog
Overview of Mediator Design Pattern

Overview of Mediator Design Pattern



Nr bE]
2 |
° fll
J ||: i

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:

Like Share Comment

Articles from Mahipal Nehra

View blog
2 years ago · 1 min. reading time

Looking back to the era where there was no internet is quite difficult. With more and more devices c ...

1 year ago · 3 min. reading time

Microservices vs Application Programming Interfaces (APIs). Both have a distinct role in software de ...

1 year ago · 1 min. reading time

We are living in a world that is on the verge of Industry 4.0 or the fourth industrial revolution. A ...

You may be interested in these jobs

  • WhiteHat Jr

    WhiteHat Jr

    Found in: Talent IN+ - 5 days ago

    WhiteHat Jr Chennai

    We're in blitz scale mode and looking for Rockstar SPM to join our journey. · JOB RESPONSIBILITIES: · Product Manager leads multiple inter-dependent work-streams, each with massive business potential and scale, immediately: · Complete end-to-end product design for web-based Ed-Te ...

  • The Mill

    DMP Artist

    Found in: Talent IN - 3 days ago

    The Mill Mumbai Full-time

    Job Description · As Digital Matte Painter you will be responsible for creating matte paintings, set extensions or concepts, which are either standalone or integrated into both photo-real and stylised live-action environments. · Qualifications · Essentials: · Should take owner ...

  • Woovly India Pvt Ltd

    Influencer Marketing Manager

    Found in: Talent IN - 5 days ago

    Woovly India Pvt Ltd Bengaluru FULL TIME

    Location - Bangalore · About the role: · Passion - influencer marketing · Interacting with people · Organizing and planning · Love social media · Develop and manage end to end influencer led campaigns including planning, building strategy, communicating brand/campaign requirement ...