Applied Design Patterns with Java
Behavioral :: Mediator (273) {C ch 20}
Example - Java :: Patterns\Behavioral\Mediator
Pattern Concept: to allow the simplification and streamlining of the intercommunication among a large group
of classes. The Mediator pattern is useful when a group of classes are so tightly coupled that their
interconnections are a tangled web, reducing extensibility and increasing maintenance. A Mediator
class promotes loose coupling by providing a common means of mediating among the classes: instead of knowing how
to communicate with each other, they only need to know how to communciate with the Mediator. Only the Mediator
class has detailed information about the other classes, and when one class needs to communicate a change to another
class, the Mediator forwards the request.
This illustration shows such a tangled set of interconnections:

The use of a Mediator streamlines and simplifies these communication interfaces:

Example - UML : MedDemo
Here is Cooper's Class Diagram for the application using this logic,
followed by the Rose equivalent:


The example Java program is called 'MedDemo'.
The UML diagram is above, and the list of Java files is
below:

Issues and consequences of the Mediator
pattern include:
- Initialization: Since the Mediator knows how to connect to and talk to all objects, it is easy and
straightforward to initialize and verify the status of all interconnected objects with a Mediator.
- Reduced Entanglement: a Mediator pattern keeps classes from becoming entangled and tightly coupled,
especially when actions in one class need to cause a a state change in another class.
- Reduced Maintenance: a Mediator makes it easy to change a program's behavior; often a change in
a class only needs to be reflected in a change to it's Mediator; the rest of the program remains unchanged.
- Ready Extensibility: new
controls or other classes can be added without changing anything but the Mediator.
- Use with Command: use of the Mediator with
a Command solves the problem of the Command object knowing too much about the objects it interacts with.
- Bloated Mediator: sometimes application
logic is put in a mediator, and it becomes a bottleneck. Mediator should only be used to facilitate intercommunication, not as a main controller.
- Reusing Mediator: If is heavily
customized, it loses generality and it can not be reused. This is a clear warning sign that Mediator bloat is occurring.
Mediator is best used when kept simple, only signalling and forwarding between mediated classes.
Catalog Behavioral Prev Next