Mediator design pattern is one of the behavioral design pattern, so it deals with the behaviors of objects. Mediator design pattern is used to provide a centralized communication medium between different objects in a system.

Mediator Design Pattern

According to GoF, mediator pattern intent is:

Allows loose coupling by encapsulating the way disparate sets of objects interact and communicate with each other. Allows for the actions of each object set to vary independently of one another.

Mediator design pattern is very helpful in an enterprise application where multiple objects are interacting with each other. If the objects interact with each other directly, the system components are tightly-coupled with each other that makes higher maintainability cost and not hard to extend. Mediator pattern focuses on provide a mediator between objects for communication and help in implementing lose-coupling between objects.

Air traffic controller is a great example of mediator pattern where the airport control room works as a mediator for communication between different flights. Mediator works as a router between objects and it can have it’s own logic to provide way of communication.

The system objects that communicate each other are called Colleagues. Usually we have an interface or abstract class that provides the contract for communication and then we have concrete implementation of mediators.

For our example, we will try to implement a chat application where users can do group chat. Every user will be identified by it’s name and they can send and receive messages. The message sent by any user should be received by all the other users in the group.

Mediator Pattern Interface

First of all we will create Mediator interface that will define the contract for concrete mediators.

Mediator Pattern Colleague Interface

Users can send and receive messages, so we can have User interface or abstract class. I am creating User as abstract class like below.

Notice that User has a reference to the mediator object, it’s required for the communication between different users.

Concrete Mediator

Now we will create concrete mediator class, it will have a list of users in the group and provide logic for the communication between the users.

Mediator Design Pattern Concrete Colleague

Now we can create concrete User classes to be used by client system.

Notice that send() method is using mediator to send the message to the users and it has no idea how it will be handled by the mediator.

Mediator Pattern Example Client Program Code

Let’s test this our chat application with a simple program where we will create mediator and add users to the group and one of the user will send a message.

Notice that client program is very simple and it has no idea how the message is getting handled and if mediator is getting user or not.

Output of the mediator pattern example program is:

Mediator Pattern Class Diagram


Mediator Pattern Example in JDK

Mediator Design Pattern Important Points

  • Mediator pattern is useful when the communication logic between objects is complex, we can have a central point of communication that takes care of communication logic.
  • Java Message Service (JMS) uses Mediator pattern along with Observer pattern to allow applications to subscribe and publish data to other applications.
  • We should not use mediator pattern just to achieve lose-coupling because if the number of mediators will grow, then it will become hard to maintain them.

That’s all for mediator design pattern and it’s implementation in java.

By admin

Leave a Reply

%d bloggers like this: