Design pattern mediator and java implementation
Written by Mottola Michele - Italy - Reggio Emilia   
Thursday, 04 December 2014 14:26
Last Updated on Thursday, 04 December 2014 14:55
AddThis Social Bookmark Button

what is the problem

I have some object and they need to communitate each other and i want a software that do it

the soluzion of mediator

The first, not best solution, is programming the objects (collegues) so that each object can communicate with all other objects.
But in this way i have hight coupling between objects. We want avoid this.
The best solution is to use an object (mediator) through each object can communicate.


class diagram

In this example i want use a simplest as possible implementation, so i use only one concrete mediator and only one concrete collegue.
The target is that i want that when a collegue change its state, all other collegue must be notify of that change through the mediator.

package mediator1;

public interface Mediator {
	public abstract void sendMessage(String s,String n,Collegue c);
	public abstract void addCollegue(Collegue c);

package mediator1;

import java.util.ArrayList;
import java.util.List;

public class ConcreteMediator implements Mediator {

	private List<Collegue> concrete;
	public void sendMessage(String s, String n, Collegue c) {
		for(Collegue coll : this.concrete){
			if(coll != c){
				String message="the "+n+" has change its status in "+s;

	public ConcreteMediator() {
		this.concrete = new ArrayList<Collegue>();
	public void addCollegue(Collegue c){

package mediator1;

public interface Collegue {
	public abstract void changeStatus(String s);
	public abstract void receiveChange(String s);

package mediator1;

public class ConcreteCollegue implements Collegue {

	private String name,status;
	private Mediator mediator;
	public void changeStatus(String s) {
		this.mediator.sendMessage(this.status,, this);


	public void receiveChange(String s) {
		System.out.println("i am ";

	public ConcreteCollegue(String name, String status, Mediator mediator) {
		super(); = name;
		this.status = status;
		this.mediator = mediator;


package mediator1;

public class Example {

	public static void main(String[] args) {
		Mediator m = new ConcreteMediator();
		Collegue a = new ConcreteCollegue("collegueA", "statusA", m);
		Collegue b = new ConcreteCollegue("collegueB", "statusB", m);
		Collegue c = new ConcreteCollegue("collegueC", "statusC", m);




i am collegueB
the collegueA has change its status in statusD
i am collegueC
the collegueA has change its status in statusD

As you can see i used the official class diagramm, eccept the method implemented.
The key point is the mediator object. It is informed from the concrete collegue when it change state. The mediator also have the responsability to inform all other collegue.
To simulate that change of state i used the method changeStatus(String).
That method invoke the sendMessage(String s, String n, Collegue c) method which has, as parameters: the new state of collegue, the name of collegue that has change its state and the object of type collegue that has change its state.
That method, then, invoke receiveChange(String) method of each other collegue. It was used to simulate the notify of state changed of a collegue.


You have no rights to post comments
You have to register to add comments