Overview of Mediator Design Pattern

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:

  1. Mediator — The mediator defines an interface for communicating between objects.
  2. Concrete Mediator — You can think of it as an implementation of the Mediator interface.
  3. 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




A place for everyone to learn about software development, data analysis, data visualization, artificial intelligence. The publication’s aim is to foster future programmers, seasoned software developers, business analysts and business owners. https://decipherzone.com

Recommended from Medium

Prometheus setup POC to production: Long term storage and HA

Create Better Data Visualizations with Python and Dash

📊Python & Dash📊: Guide to Better Data Visualization

Is It Too Late to Earn Money Mining Helium?

How to optimise automatic speech recognition performance using MKL

Cloud Native Design Pattern — Implementing in Pega PRPC

Announcement:Our First Live Q&A session

10 Efficient Ways to Use Python Lists

How Does Rails (and Rack) Parse Form Variables?

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mahipal Nehra

Mahipal Nehra

Working in a leading outsourcing Java development company

More from Medium

Design Patterns: Singleton Pattern

(Unknown): Script Error in JavaScript

Push Notifications using WebSocket with Spring Boot and React(Part-1)

Singleton Design pattern in Java