Mediator design pattern

22 Feb, 2018
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 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.
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.
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
This article is contributed by Saket Kumar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@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.
Design Pattern
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.