Mediator Design Pattern

Last modified on August 1st, 2014 by Joe.

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

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.

Comments on "Mediator Design Pattern"

  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……….

  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. 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.

  6. 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 ?

  7. GOOTAM says:

    Good Work Joe. Keep it up.

  8. Koray Tugay says:

    Very good..

  9. 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…

  10. Bhimaray says:

    Hello sir, you explained very nicely

  11. Muhammad Zahab Ahmed says:

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

  12. Anonymous says:

    very nice sir jee

  13. zeinab says:

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

  14. amjad says:

    thanks, for your nice explanation

  15. Praveen says:

    example is very help-full, Good work Joe.

  16. Nitin says:

    Great article….

  17. sayras says:

    very nice explanation

    thanks

  18. 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.

  19. Shrikant Herwade says:

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

  20. Tamilarasu K says:

    Really Nice

  21. 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.

  22. Anonymous says:

    nice..explanation

  23. Ankit says:

    very nice tutorials. Keep it up.

  24. Mahesh says:

    Really helpful Joe

  25. Ravindra says:

    Nicely presented with very good examples.

  26. dhrumil says:

    nicely explain

  27. yashwant says:

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

  28. 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.

  29. Anonymous says:

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

  30. 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.

  31. padmarao says:

    hi ,joe
    please share the answer to my querys,

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

  32. Eswar1221 says:

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

  33. Jay says:

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

  34. 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?

Comments are closed for "Mediator Design Pattern".