Mediator Design Pattern | Java Implementation

Mediator pattern is a Behavioral Design pattern. It is used to handle complex communications between related Objects, helping by decoupling those objects.

Intent

Mediator pattern defines an object that encapsulates how a set of objects interact. Mediator promotes loose coupling of Objects by keeping them from referring to each other directly, and it lets you vary their interaction independently.

So, simply said, it basically lets you control the communication between each object. As the name implies: mediator.

Mediator Pattern is useful for Enterprise Applications where multiple modules are communicating with each other, If the modules interact with each other directly, then the modules will be tightly coupled. As a result, maintenance of these modules will be more difficult and make them non extendable.

Real World Example

This pattern defines a mediator object that controls how a set of objects interacts. The objects which are communicating via the mediator are called as colleague objects. Loose coupling between colleague objects is achieved by having colleagues communicate with the Mediator, rather than with each other.

An airport control tower is an excellent example of the mediator pattern. The pilots of the arriving planes or departing the terminal area communicate with the tower rather than explicitly communicating with one another. Here the control tower is acting as a mediator and the planes are the colleague objects. This idea of a central controller is one of the key aspects of the mediator pattern.

Class Diagram

As we have already discussed that mediator pattern relies on a central controller aka mediator and the colleague objects. The colleague objects need to communicate with each other which is accomplished via the mediator. Now let’s have a look in the class diagram. The key player in Mediator pattern are the mediator itself and the colleague classes. The mediator defines an interface as an abstraction. This abstraction layer is used for communicating with the colleague objects. The concrete mediator implements cooperative behavior by coordinating with the colleague objects. Next we have our colleague classes. Each colleague class knows the mediator object and each colleague communicates with its mediator only when it requires to communicate with other colleague objects.

Java Implementation of Mediator Pattern:

Now let us understand this pattern with a real implementation. The chat box application is an example of the mediator pattern. Several participants can join a chat box for discussion on some topic. Now it is not feasible or scalable to connect each participant to all other participant in that chat-box. In such scenario the number of connections will be too high. To solve this problem, we can use the mediator pattern, where the chat-box is the mediator and all the participants are the colleague objects. The participants never communicate directly to each other, they will interact via the chat-box when required. Lets check the implementation here:

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