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.

example

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.

Mediator.java

package mediator1;

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

ConcreteMediator.java

package mediator1;

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

public class ConcreteMediator implements Mediator {

	private List<Collegue> concrete;
	@Override
	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;
				coll.receiveChange(message);
			}
		}

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

Collegue.java

package mediator1;

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

ConcreteCollegue.java

package mediator1;

public class ConcreteCollegue implements Collegue {

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

	}

	@Override
	public void receiveChange(String s) {
		System.out.println("-----------------------");
		System.out.println("i am "+this.name);
		System.out.println(s);
	}

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

}

Example.java

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

		m.addCollegue(a);
		m.addCollegue(b);
		m.addCollegue(c);
		
		a.changeStatus("statusD");
		
	}

}

Output

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

 

Add comment

Security code
Refresh