Mediator Design Pattern

Behavioral design pattern can change the way programs run. 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.

In an enterprise application where we have large number of classes the complexity also increases proportionately. Business logic might be spread across those multiple classes. There will be interaction between classes. If we draw line between classes where we have interaction, then it will end up like Idiyappam (Indian rice noodle).

idiyappam

Before Mediator Design Pattern

So what is the problem we are trying to solve here? This complex interaction between objects creates dependency and tighter coupling. We stand for loose coupling, we want to reduce dependency as much as possible, we want more reuse.

MediatorProblem

After Mediator Design Pattern

Define an object that acts as a mediator between communicating objects thus removing the direct dependency between those communicating objects. This mediator object encapsulates the interaction information and uses it to enable communication between the objects.

Mediator

The intermediate mediator object is not only to communicate between two objects, but it helps in interaction for a set of peer objects. This mediator object is like a router in networks, which is routes / controls or coordinates communication between systems.

Real-time Example for Mediator Design Pattern

Air traffic controller (ATC) is a mediator between flights. It helps in communication between flights and co-oridates/controls landing, take-off. Two flights need not interact directly and there is no dependency between them. This dependency is solved by the mediator ATC. If ATC is not there all the flights have to interact with one another and managing the show will be very difficult and things may go wrong.

ATC Mediator

Mediator Pattern Implementation

In a mediator design pattern implementation we will have

  • mediator interface – an interface that defines the communication rules between objects
  • concrete mediator – a mediator object which will enables communication between participating objects
  • colleague – objects communicating with each other through mediator object

Java Example for Mediator Desing Pattern

package com.javapapers.designpattern.mediator;

public 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;

	}
}

package com.javapapers.designpattern.mediator;

public interface Command {
	void land();
}

package com.javapapers.designpattern.mediator;

public interface IATCMediator {

	public void registerRunway(Runway runway);

	public void registerFlight(Flight flight);

	public boolean isLandingOk();

	public void setLandingStatus(boolean status);
}

package com.javapapers.designpattern.mediator;

public class Flight implements Command {
	private IATCMediator atcMediator;

	public Flight(IATCMediator atcMediator) {
		this.atcMediator = atcMediator;
	}

	public void land() {
		if (atcMediator.isLandingOk()) {
			System.out.println("Landing done....");
			atcMediator.setLandingStatus(true);
		} else
			System.out.println("Will wait to land....");
	}

	public void getReady() {
		System.out.println("Getting ready...");
	}

}

package com.javapapers.designpattern.mediator;

public 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);
	}

}

MediatorDesignPattern.java

package com.javapapers.designpattern.mediator;

public 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();
	}
}

Mediator Design Pattern in Java API

java.util.Timer uses the mediator design pattern.

This Behavioral Design Pattern tutorial was added on 31/01/2013.

Comments on "Mediator Design Pattern" Tutorial:

  1. Jayani says:

    Hello Joe sir,
    You have Nicely explained with good example!

    Can we say JMS(Java Messaging System) is a combination of Observer and Mediator design patterns?

  2. Shailesh says:

    nice article ….

  3. C SATHIYARAJA says:

    Sir i want java mail concepts for received mail detail.. and reply mail edit before sent concepts……file comparision for all received mail…..Are you free means contact immediately for my mail id……….

    • Karesh says:

      Mr. C SATHIYARAJA,

      What do you want Joe to implement everything for you?? He can give you hints and suggestions. Ask right questions here.

  4. Mohamed Galala says:

    You are really explaining the patterns in a very clear way.
    Thank you for your effort.
    I have only two comment:-
    1- Your java example needs to be provided with comments.
    For example, saying before the interface implementation that this is the Mediator Interface and so on.
    2- Keep in mind the order of code snippets you are writing. For example, the mediator interface implementation must come before the implementation class.

    Thank you one more time.

  5. Senthil Muthiah says:

    Hi Joe
    I like all your post and the way how you are presenting to the users.

    And also i like the your site theme, I just want to create website like this. Any help on this ?

  6. GOOTAM says:

    Good Work Joe. Keep it up.

  7. Koray Tugay says:

    Very good..

  8. Nareshreddy Kola says:

    Thanks for the nice explanation…In ATCMediator you have declared land with public where as it could be declared as private. Correct me if i miss anything…

  9. Bhimaray says:

    Hello sir, you explained very nicely

  10. Muhammad Zahab Ahmed says:

    You have explained very nicely…i enjoyed your blogs alot…Thanksss….

  11. Anonymous says:

    very nice sir jee

  12. zeinab says:

    hi, i didnt find Sterategy design pattern in yr blog, can u explain that please?
    thanks in advance

  13. amjad says:

    thanks, for your nice explanation

  14. Praveen says:

    example is very help-full, Good work Joe.

  15. Nitin says:

    Great article….

  16. sayras says:

    very nice explanation

    thanks

  17. aprajitha says:

    Awesome real time example (Air Traffic Controller) to explain the pattern. Examples like these will last in memory forever than any set of code examples.

  18. Shrikant Herwade says:

    You really explain it in a better way..! simple n sober…Best Luck for future sharings…

  19. Tamilarasu K says:

    Really Nice

  20. mayur says:

    Can you explained the which interfece is mediator interface ,concrete mediator & colleague in above example.How they interact for multiple flight with single runway.

  21. Anonymous says:

    nice..explanation

  22. Ankit says:

    very nice tutorials. Keep it up.

  23. Mahesh says:

    Really helpful Joe

  24. Ravindra says:

    Nicely presented with very good examples.

  25. dhrumil says:

    nicely explain

  26. yashwant says:

    Hey joe !! Very nice & simple explanation. Keep it up !!

  27. Rahini Mariasingam says:

    Hi Joe

    Thanks for your java blog . It is very helpful and simple and easy explanation for each design pattern.
    Keep up your good work.

  28. Anonymous says:

    Best stuff here. Superb sir. Thanks for your support by explaining such a important concepts in so easy way

  29. Ahmed says:

    First of all thanks for the blog, Actually, I was recalling all the design patterns to solve a specific design problem in my current project, and Mediator was one of the patterns that stopped me, but the question how easy can we write a JUnit test for the Flight or Runway.

    Do we have to create a mockup for the Mediator, because all of the classes became tightly coupled to the Mediator, may be it’s an invalid assumption and may be not, your inputs are appreciated.

  30. padmarao says:

    hi ,joe
    please share the answer to my querys,

    why spring bean is by default singleton?
    and cache design pattern explain………please

  31. Eswar1221 says:

    Hi Joe,
    private Flight flight;
    private Runway runway; are unused in this example. Why So ?

  32. Jay says:

    Good job… JOE.. nice explanation.. keep up the good work..

  33. Abhinav Saxena says:

    atcMediator.setLandingStatus(true);

    in

    public Runway(IATCMediator atcMediator) {
    this.atcMediator = atcMediator;
    atcMediator.setLandingStatus(true);
    //should not it be false initially?
    }

    question is: why this should not be:

    atcMediator.setLandingStatus(false);

    why the landing permission is granted in constructor when it should be granted only when the Runway is actually cleared in the future i.e. in the method land() when it is called?

Your Comment

↑ Go to top