Mediator comes under Behavioral design pattern.
It defines an interface for communication among Colleague objects
and encapsulates the communication logic.
Behaviour & Advantages
Encapsulates the communication logic.
Mediator promotes loose coupling by keeping objects from referring to each other explicitly.
Colleague objects need not know each other explicitly.
Participants
Mediator
Abstract interface for communication among Colleague objects.
Concrete Mediator
An implementation of Mediator which knows all Colleague objects.
It keeps a reference to Colleagues and coordinates communication among them.
Colleague
Abstract interface for Colleagues.
Concrete Colleague
Objects which require communication with other Colleagues.
It keeps a reference to Mediator and communicates with other Colleagues.
This example shows an organization where Employees like to communicate
each other regarding a marriage invitation through a Mediator.
Each employee doesn't require to know all other Employees directly.
/**
* Broadcasts message to an employee if his salary
* is greater than the message sender.
*
* In mediator pattern the communication between
* objects is encapsulated and driven by mediator.
*/ public class RichEmployeeMediator implements IMediator {
private List<IEmployee> empList = new ArrayList<IEmployee>();
@Override public void register(IEmployee [] colleagues) {
for (int i = 0 ; i < colleagues.length ; i ++ ) {
colleagues[i].setMediator(this);
empList.add(colleagues[i]);
}
}
@Override public void sendMessage(IEmployee fromColleague, String message) {
for (int i = 0 ; i < empList.size() ; i ++) { if (empList.get(i).getSalary() > fromColleague.getSalary()) {
empList.get(i).onMessage(message);
}
}
}
}
A Concrete mediator implementation which invites only the employees earning less
salary than the message sender.
/**
* Broadcasts message to an employee if his salary
* is less than the message sender.
*
* In mediator pattern the communication between
* objects is encapsulated and driven by mediator.
*/ public class PoorEmployeeMediator implements IMediator {
private List<IEmployee> empList = new ArrayList<IEmployee>();
@Override public void register(IEmployee [] colleagues) {
for (int i = 0 ; i < colleagues.length ; i ++ ) {
colleagues[i].setMediator(this);
empList.add(colleagues[i]);
}
}
@Override public void sendMessage(IEmployee fromColleague, String message) {
for (int i = 0 ; i < empList.size() ; i ++) { if (empList.get(i).getSalary() < fromColleague.getSalary()) {
empList.get(i).onMessage(message);
}
}
}
CEO emp1 = new CEO("RAM", 128000);
Manager emp2 = new Manager("RAJ", 48000);
Manager emp3 = new Manager("KIRAN", 28000);
Employee emp4 = new Employee("ARUN", 16000);
Employee emp5 = new Employee("PRASAD", 44000);
Employee emp6 = new Employee("MANO", 9000);
Employee emp7 = new Employee("ARJUN", 14000);
Employee emp8 = new Employee("ABHI", 12000);
/**
* Send invitation to all rich employees than sender.
*/
IMediator mediator = new RichEmployeeMediator();
mediator.register(new Employee [] {
emp1, emp2, emp3, emp4, emp5, emp6, emp7, emp8
});
emp4.sendMarriageInvitation();
System.out.println();
/**
* Send invitation to all poor employees than sender.
*/
mediator = new PoorEmployeeMediator();
mediator.register(new Employee [] {
emp1, emp2, emp3, emp4, emp5, emp6, emp7, emp8
});
emp4.sendMarriageInvitation();
}
}
It gives the following output,
INBOX(CEO[RAM, 128000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Manager[RAJ, 48000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Manager[KIRAN, 28000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Employee[PRASAD, 44000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Employee[MANO, 9000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Employee[ARJUN, 14000]) : Please come to my marriage - by Employee[ARUN, 16000]
INBOX(Employee[ABHI, 12000]) : Please come to my marriage - by Employee[ARUN, 16000]