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

About The Author

I worked with various Insurances companies across Switzerland on online applications handling billion premium volumes. I love to continuously spark my creativity in many different and challenging open-source projects fueled by my great passion for innovation and blockchain technology.In my technical role as a senior software engineer and Blockchain consultant, I help to define and implement innovative solutions in the scope of both blockchain and traditional products, solutions, and services. I can support the full spectrum of software development activities, starting from analyzing ideas and business cases and up to the production deployment of the solutions.I'm the Founder and CEO of Disruptr GmbH.

Categories