Open In App
Related Articles

Mediator design pattern

Improve Article
Improve
Save Article
Save
Like Article
Like

Mediator design pattern is one of the important and widely used behavioral design pattern. Mediator enables decoupling of objects by introducing a layer in between so that the interaction between objects happen via the layer. 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 providing a mediator between objects for communication and help in implementing loose-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.

UML Diagram Mediator design pattern

Design components

  • Mediator :It defines the interface for communication between colleague objects.
  • ConcreteMediator : It implements the mediator interface and coordinates communication between colleague objects.
  • Colleague : It defines the interface for communication with other colleagues
  • ConcreteColleague : It implements the colleague interface and communicates with other colleagues through its mediator

Let’s see an example of Mediator design pattern. 

Java




class ATCMediator implements IATCMediator
{
    private Flight flight;
    private Runway runway;
    public boolean land;
 
    public void registerRunway(Runway runway)
    {
        this.runway = runway;
    }
 
    public void registerFlight(Flight flight)
    {
        this.flight = flight;
    }
 
    public boolean isLandingOk()
    {
        return land;
    }
 
    @Override
    public void setLandingStatus(boolean status)
    {
        land = status;
    }
}
 
interface Command
{
    void land();
}
 
interface IATCMediator
{
 
    public void registerRunway(Runway runway);
 
    public void registerFlight(Flight flight);
 
    public boolean isLandingOk();
 
    public void setLandingStatus(boolean status);
}
 
class Flight implements Command
{
    private IATCMediator atcMediator;
 
    public Flight(IATCMediator atcMediator)
    {
        this.atcMediator = atcMediator;
    }
 
    public void land()
    {
        if (atcMediator.isLandingOk())
        {
            System.out.println("Successfully Landed.");
            atcMediator.setLandingStatus(true);
        }
        else
            System.out.println("Waiting for landing.");
    }
 
    public void getReady()
    {
        System.out.println("Ready for landing.");
    }
 
}
 
class Runway implements Command
{
    private IATCMediator atcMediator;
 
    public Runway(IATCMediator atcMediator)
    {
        this.atcMediator = atcMediator;
        atcMediator.setLandingStatus(true);
    }
 
    @Override
    public void land()
    {
        System.out.println("Landing permission granted.");
        atcMediator.setLandingStatus(true);
    }
 
}
 
class MediatorDesignPattern
{
    public static void main(String args[])
    {
 
        IATCMediator atcMediator = new ATCMediator();
        Flight sparrow101 = new Flight(atcMediator);
        Runway mainRunway = new Runway(atcMediator);
        atcMediator.registerFlight(sparrow101);
        atcMediator.registerRunway(mainRunway);
        sparrow101.getReady();
        mainRunway.land();
        sparrow101.land();
         
    }
}


Output:

Ready for landing.
Landing permission granted.
Successfully Landed.

Advantage

  • It limits subclassing. A mediator localizes behavior that otherwise would be distributed among several objects. Changing this behaviour requires subclassing Mediator only, Colleague classes can be reused as is.

Disadvantage

  • It centralizes control. The mediator pattern trades complexity of interaction for complexity in the mediator. Because a mediator encapsulates protocols, it can become more complex than any individual colleague. This can make the mediator itself a monolith that’s hard to maintain

If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Feeling lost in the vast world of System Design? It's time for a transformation! Enroll in our Mastering System Design From Low-Level to High-Level Solutions - Live Course and embark on an exhilarating journey to efficiently master system design concepts and techniques.
What We Offer:
  • Comprehensive Course Coverage
  • Expert Guidance for Efficient Learning
  • Hands-on Experience with Real-world System Design Project
  • Proven Track Record with 100,000+ Successful Enthusiasts

Last Updated : 31 Oct, 2023
Like Article
Save Article
Previous
Next
Similar Reads