Select Page

Mediator

Mediator

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);}  
0 0 votes
Article Rating
Subscribe
Notify of
0 Comments
Inline Feedbacks
View all comments

Categories

0
Would love your thoughts, please comment.x
()
x