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).
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.
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.
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.
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.
In a mediator design pattern implementation we will have
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); } }
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(); } }
java.util.Timer uses the mediator design pattern.
Comments are closed for "Mediator Design Pattern".
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?
nice article ….
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……….
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.
Mr. C SATHIYARAJA,
What do you want Joe to implement everything for you?? He can give you hints and suggestions. Ask right questions here.
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 ?
Good Work Joe. Keep it up.
Very good..
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…
Hello sir, you explained very nicely
You have explained very nicely…i enjoyed your blogs alot…Thanksss….
very nice sir jee
hi, i didnt find Sterategy design pattern in yr blog, can u explain that please?
thanks in advance
thanks, for your nice explanation
example is very help-full, Good work Joe.
Great article….
very nice explanation
thanks
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.
You really explain it in a better way..! simple n sober…Best Luck for future sharings…
Really Nice
Can you explained the which interfece is mediator interface ,concrete mediator & colleague in above example.How they interact for multiple flight with single runway.
nice..explanation
very nice tutorials. Keep it up.
Really helpful Joe
Nicely presented with very good examples.
nicely explain
Hey joe !! Very nice & simple explanation. Keep it up !!
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.
Best stuff here. Superb sir. Thanks for your support by explaining such a important concepts in so easy way
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.
hi ,joe
please share the answer to my querys,
why spring bean is by default singleton?
and cache design pattern explain………please
Hi Joe,
private Flight flight;
private Runway runway; are unused in this example. Why So ?
Good job… JOE.. nice explanation.. keep up the good work..
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?