Mediator behavioral pattern
The Mediator pattern suggests that you should cease all direct communication between the components which you want to make independent of each other.
Instead, these components must collaborate indirectly, by calling a special mediator object that redirects the calls to appropriate components.
As a result, the components depend only on a single mediator class instead of being coupled to dozens of their colleagues.
When should you use it ?
- Use the Mediator pattern when it’s hard to change some of the classes because they are tightly coupled to a bunch of other classes.
- Use the Mediator when you find yourself creating tons of component subclasses just to reuse some basic behavior in various contexts.
- Use the pattern when you can’t reuse a component in a different program because it’s too dependent on other components.
How do you implement it ?
- Identify a group of tightly coupled classes which would benefit from being more independent (e.g., for easier maintenance or simpler reuse of these classes).
- Declare the mediator interface and describe the desired communication protocol between mediators and various components. In most cases, a single method for receiving notifications from components is sufficient.
- This interface is crucial when you want to reuse component classes in different contexts. As long as the component works with its mediator via the generic interface, you can link the component with a different implementation of the mediator.
- Implement the concrete mediator class. This class would benefit from storing references to all of the components it manages.
- You can go even further and make the mediator responsible for the creation and destruction of component objects. After this, the mediator may resemble a factory or a facade.
- Components should store a reference to the mediator object. The connection is usually established in the component’s constructor, where a mediator object is passed as an argument.
- Change the components’ code so that they call the mediator’s notification method instead of methods on other components. Extract the code that involves calling other components into the mediator class. Execute this code whenever the mediator receives notifications from that component.