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

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

3 years ago · 1 min. reading time

Top 10 IT skills that will be in demand in 2021. Every passing year the tech world grows and transfo ...

3 years ago · 2 min. reading time

Machine Learning (ML) and Artificial Intelligence (AI) technologies have gained increasing popularit ...

You may be interested in these jobs

  • SiClarity Software Solutions Pvt Ltd

    Machine Learning Engineer

    Found in: Appcast Linkedin IN C2 - 21 hours ago


    SiClarity Software Solutions Pvt Ltd Mumbai, India

    About SiClarity Software Solutions Pvt Ltd. · SiClarity is a US-based organization with Headquarters in Palo Alto, CA. · We are in the Electronic Design Automation (EDA) domain catering to the modern needs of the semiconductor chip design and fabrication industry. Our software so ...

  • Multplyr

    Angular Developer

    Found in: Appcast Linkedin IN C2 - 21 hours ago


    Multplyr Chennai, India

    Hiring for:A well established and rapidly scaling Housing Finance Company with a target AUM of 10K Crore in FY23-24. · Location: Chennai · Joining : Immediate to 15 days preferred · No. of Positions: 4 · Requirements: · -Degree in computer science. · -Proven experience as a Front ...

  • OrbiTouch-HR (orbiTouch outsourcing pvt ltd)

    Regional Sales Manager

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


    OrbiTouch-HR (orbiTouch outsourcing pvt ltd) Bengaluru, India

    Urgently Hiring for Regional Sales Manager|| Bangalore|| · Profile- Regional Sales Manager · Experience-7 years · Ctc- 12 lpa · Location- Bangalore · Working Days- 6 days (2nd & 4th Saturday off) · Key Responsibilities: · Prospecting:- · Developing a Comprehensive DATA BASE of c ...