- Summary
- Purpose of this pattern is to removed Peer to Peer
object coupling.
- 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.
- Sole purpose if to define integrated
communication channel in between several coordinating classes and avoid direct
communication
- Overview Tutorials
- http://www.oodesign.com/mediator-pattern.html
- Diagram
- Specific problems and implementation
- Abstract Vs Concrete Mediators
- 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.
- The
definition of an abstract Mediator is required only if the colleagues needs to
work with different mediators.
- Communication between mediators and colleagues
- There are different ways to realize the
communication between the colleagues and its mediator:
- 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.
- 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
- 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
- Complexity of Mediator object
- 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.
- Pros Vs Cons
- Advantages
- 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.
- Decoupled
Colleagues - The colleague classes are totally decoupled.
Adding a new colleague class is very easy due to this decoupling level.
- 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.
- 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.
- Disadvantages
- 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.
- Related Patterns
- 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.
- 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.
- 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.
|
|