Mediator Design Pattern in Java

By | July 6, 2020

In this post, We will talk and learn about the Mediator Design Pattern in Java.

Key Points About Mediator Design Pattern :

  • Mediator design pattern falls under behavioral design pattern. This Pattern deals with the behaviors of objects. This pattern is used to provide a centralized communication medium between different objects in the application.
  • This pattern is really very helpful in an enterprise application where multiple objects are interacting with each other. If the objects interact with each other directly than system components are tightly-coupled with each other that make higher maintainability cost and not hard to extend as well. The mediator pattern usually focuses to provide a mediator between objects for communication and helps in implementing lose-coupling between objects.
  • In Mediator design pattern the system objects that communicate with each other are called Colleagues. Usually, we have an interface or abstract class that provides the contract for communication and then we need to create a concrete implementation of mediators.
  • Mediator pattern is really useful when the communication logic between objects is complex and we can have a central point of communication that takes care of communication logic.

Mediator Pattern Example in JDK:

Now Let’s move towards the implementation of the Mediator Design Pattern.

Here, Imeplemenation Shows an example of Group Chat Application where every user can be identified by their user name, and when one user sends a message in group than that message should be received by all other users in that group Chat.

Below is the complete source code:

Mediator Design Pattern

ChattingMediator.java

 

ChattingMediatorImpl.java

 

User.java

 

UserImpl.java


ClientTest.java

Output of this Program:

PK sending message = Hi,All
KK received message = Hi,All
MK received message = Hi,All
SK received message = Hi,All
VK received message = Hi,All

Above output shows message send my User: PK, Received all other Users in Group Chat

You May Also Like:

Singleton Design Pattern in java
Prototype Pattern in Java
Factory Pattern in Java
Abstract Factory Pattern in Java
Builder Pattern in Java
Adapter Design Pattern in Java
Decorator Design Pattern in Java
Facade Design Pattern in Java
Bridge Design Pattern in Java.
Chain of Responsibility Design Pattern in Java
Template Design Pattern in Java

That’s all about the Mediator Design Pattern in Java.
If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

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