|
Thursday, 20 January 2005 21:38 |
Define an object that encapsulates how a set of objects interact. Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

Source code/**
* Each colleague knows its Mediator object. Communicates with its mediator
* whenever it would have otherwise communicated with another colleague.
*
* @role __Colleague
*/
public abstract class Colleague {
/** my mediator */
private Mediator mediator; /** Create colleague which knows about supplied mediator */
protected Colleague(Mediator mediator) {
this.mediator = mediator; }
/** @return mediator this colleague knows about */
public Mediator getMediator() {
return mediator; }
}
/** Concrete Colleague */
public class ConcreteColleagueA extends Colleague {
public ConcreteColleagueA(Mediator mediator) {
super(mediator); }
public void sampleOperation() {
// some state changes occur,
// notify mediator about them
getMediator().changed(this); }
}
/** Concrete Colleague */
public class ConcreteColleagueB extends Colleague {
public ConcreteColleagueB(Mediator mediator) {
super(mediator); }
public void sampleOperation() {
// some state changes occur,
// notify mediator about them
getMediator().changed(this); }
}
/**
* Implements cooperative behavior by coordinating Colleague objects. Knows and
* maintains its colleagues.
*/
public class ConcreteMediator implements Mediator {
/** reference to concrete colleague */
private ConcreteColleagueA aConcreteColleagueA; /** reference to concrete colleague */
private ConcreteColleagueB aConcreteColleagueB; public void changed(Colleague colleague) {
// handle changes of particular colleague
}
public void setConcreteColleagueA(ConcreteColleagueA colleague) {
aConcreteColleagueA = colleague; }
public void setConcreteColleagueB(ConcreteColleagueB colleague) {
aConcreteColleagueB = colleague; }
}
/**
* Defines an interface for communicating with Colleague objects.
*
* @role __Mediator
*/
public interface Mediator {
/** Colleagues calls this method to notify Mediator that something changed */
void changed(Colleague colleague);}
|
|
Last Updated ( Friday, 17 June 2005 23:39 )
|
Anyone has a tutorial for developing ...
what type of antifreeze do i use in m...
css lessons - css scrollbar examples ...
SILLY
Now I can stand at the station watchi...