1.Topics‎ > ‎Architecture‎ > ‎Patterns‎ > ‎a.Design Patterns‎ > ‎D.Behavioral Patterns‎ > ‎

Mediator Pattern

  1. Summary
    1. Purpose of this pattern is to removed Peer to Peer object coupling. 
      1. 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.
      2. Sole purpose if to define integrated communication channel in between several coordinating classes and avoid direct communication
  2. Overview Tutorials 
    1. http://www.oodesign.com/mediator-pattern.html
  3. Diagram

  4. Specific problems and implementation
    1. Abstract Vs Concrete Mediators
      1. There is no need to create an Abstract Mediator class or an interface as long as the colleagues are going to use only one mediator.
      2. The definition of an abstract Mediator is required only if the colleagues needs to work with different mediators.
    2. Communication between mediators and colleagues
      1. There are different ways to realize the communication between the colleagues and its mediator:
        1.  One of the most used methods is to use the Observer pattern. The mediator can be also an observer and the Colleagues can be implement an observable object. Each time an change is made in the state of the observable object, the observer(mediator) gets notified and it notify all other colleagues objects.
        2. Alternative methods can be used to send messages to the mediator. For example a simple delegation can be used and specialised methods can be exposed by the mediator
        3. In more complex implementations asynchronous messages can be added to to a message queue, from where they can be picked up by the mediator object
    3. Complexity of Mediator object
      1. One potential problem is complexity of the mediator when the number of participants is a high and the different participant classes is high. If you created custom dialog for GUI applications you remember that after some time the dialog classes become really complex because they had to manage a lot of operations.
    4. Pros Vs Cons
      1. Advantages
        1. Comprehension - The mediator encapsulate the logic of mediation between the colleagues. From this reason it' more easier to understand this logic since it is kept in only one class.
        2. Decoupled Colleagues - The colleague classes are totally decoupled. Adding a new colleague class is very easy due to this decoupling level.
        3. Simplified object protocols - The colleague objects need to communicate only with the mediator objects. Practically the mediator pattern reduce the required communication channels(protocols) from many to many to one to many and many to one.
        4. Limits Subclassing - Because the entire communication logic is encapsulated by the mediator class, when this logic need to be extended only the mediator class need to be extended.
      2. Disadvantages
        1. Complexity - in practice the mediators tends to become more complex and complex. A good practice is to take care to make the mediator classes responsible only for the communication part. For example when implementing different screens the the screen class should not contain code which is not a part of the screen operations. It should be put in some other classes.
  5. Related Patterns
    1. Facade Pattern - a simplified mediator becomes a facade pattern if the mediator is the only active class and the colleagues are passive classes. A facade pattern is just an implementation of the mediator pattern where mediator is the only object triggering and invoking actions on passive colleague classes. The Facade is being call by some external classes.
    2. Adapter Pattern - the mediator patter just "mediate" the requests between the colleague classes. It is not supposed to change the messages it receives and sends; if it alters those messages then it is an Adapter pattern.
    3. Observer Pattern - the observer and mediator are similar patterns, solving the same problem. The main difference between them is the problem they address. The observer pattern handles the communication between observers and subjects or subject. It's very probable to have new observable objects added. On the other side in the mediator pattern the mediator class is the the most likely class to be inherited.