Other Tutorials

Mediator Design Pattern In Java

Introduction:

In this tutorial, we’ll learn about a behavioral pattern that promotes loose coupling between several objects communicating with one another. The idea behind the Mediator design pattern is to have a central object that encapsulates how a set of objects interact.

In the mediator pattern, we extract all the relationships between different classes in a separate class known as a mediator. This enables us to make changes to one component without impacting the entire system.

And so, we have a more loosely-coupled system that’s easier to extend and maintain.

UML Representation:

We can represent the mediator pattern with the help of following UML diagram:

Mediator design pattern

Here, we have:

  • Mediator: an interface or an abstract class defining the contract for communication among colleagues
  • ConcreteMediator: a class that implements the mediator contract; it is aware of all colleagues and their inter-communications. Any communication among colleagues happens only through a mediator
  • Colleague: an interface or an abstract class representing components of our system
  • ConcreteColleague: classes that implement the Colleague interface and are willing to interact with one another

Implementing the Mediator Pattern:

Let’s take the example of an aerospace traffic control system.

Each flight needs to know about the available runway for its landing. If we allow inter-communication among the aircraft for them to find the available runway, it will lead to chaos. Rather, it’s a good idea to have an aircraft traffic control room which keeps track of all available runways and assigns them to an aircraft.

Let’s start by defining an AircraftMediator and the AircraftTrafficControlRoom:

The air traffic control room acts as a mediator and keeps track of all available runways. It’s responsible to allot and release runways.

Defining Colleagues:

Now, let’s define our Aircraft, the instances of which will be the colleagues:

Testing Our Implementation:

Let’s see how this works together:

On executing the above code, we’ll have the following output:

If we again attempt a landing of the airbus, we’ll be able to proceed with it as the runway is now available.

Conclusion:

In this tutorial, we learned how to implement a mediator pattern. We’ll use a mediator pattern when we have a system in which multiple objects communicate with each other. This pattern promotes loose coupling and thereby makes the system more flexible.

A common application of this pattern is a chat or messaging system. Moreover, the Java Concurrency Executor’s execute() method makes use of this pattern.

Be the First to comment.

Leave a Comment

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