Define an object that encapsulates relations between a set of objects to make them independent of each other.
Say you have a dialog for editing user profiles. It consists of various form fields such as
Some form elements have to interact with others. For instance, the "I have a dog" checkbox may show a hidden text field for entering the dog's name. Another example is the submit button that has to validate values of all fields before saving data.
By placing this logic directly into the form elements code, you make it much harder to reuse these classes in other forms of the app. For example, you can't use a checkbox inside another form, since it tightly coupled to the dog's text field. You can either use all classes or none at all.
The Mediator pattern prevents direct communications between individual components. Instead, they send requests to a mediator object that knows whom to direct these requests. Components lose dozens of dependencies and become related only to the mediator object.
In our example, the dialog class may represent a mediator. Probably, you won't even need to add any new dependencies into the class, since the dialog already knows about its child form elements.
The most significant change will happen inside form elements code. For example, let's consider the submit button. Previously, it had to be responsible for validating individual form elements when clicked. But now, its single job would be to just notify the dialog about the click. Upon receiving a submit notification, the dialog would do all the validations by itself. This way, instead of dozen relations to other form elements, the button will only be dependent on the dialog class.
But you can go further and make this dependency even looser by extracting the common interface for all dialogs. The interface would define a method that will be used by all form elements to notify mediator about their events. Thus, our submit button will be able to communicate with any dialog that follows that interface.
This way the Mediator pattern wraps a web of relations between various components inside a single mediator object. The fewer dependencies has a class, the easier it becomes to modify, extend or reuse it.
Air traffic control
The control tower at a controlled airport demonstrates the Mediator pattern very well. The pilots of the planes approaching or departing the terminal area communicate with the tower rather than explicitly communicating with each other. The constraints on who can take off or land are enforced by the tower.
It is important to note that the tower does not control the whole flight. It exists only to enforce constraints in the terminal area.
Components are various objects containing business logic of a program. Each component stores a reference to a mediator. Components neither know nor care about the mediator's actual type since they work with it through the Mediator interface. This allows reusing components in other programs by linking them to a different mediator.
Components are not aware of other components. When their state change, they notify the mediator. Mediator knows whether any other component should do something about the event.
Mediator defines the interface of communication with Components. In most cases, it is a single method that's called when some event happens to a component. The component may pass itselves and the event details as arguments of this method.
This interface is crucial for reusing existing components in other programs or contexts. All you need to do is to implement a new Concrete Mediator that will capture a new interaction logic and pass it to the components.
Concrete Mediator encapsulates relations between various components. Mediators often keep references to all components they manage.
Concrete mediators are usually tightly coupled to their components. When mediator receives a notification, it can easily identify a sender. It also knows which component should be triggered in return. But from the component perspective it's a total black box. The sender doesn't know who end-up handling its event and the receiver doesn't know who issued the event in the first place.
In this example, the Mediator pattern helps to eliminate mutual dependencies between UI classes: buttons, checkboxes and text labels. When a user interacts with elements, they don't communicate directly, but rather notify a mediator about the event.
The authentication dialog acts as mediator. It knows how concrete elements should behave together and facilitates their cooperation. Upon receiving an event notification, it passes execution to the appropriate components.
// Common mediator interface. interface Mediator is method notify(type: string, sender: Component) // Concrete mediator. All chaotic communications between concrete components // have been extracted to the mediator. Now components only talk with the // mediator, which knows who has to handle a request. class AuthenticationDialog implements Mediator is field title: string field loginOrRegister: Checkbox field loginUsername, loginPassword: Textbox field registrationUsername, registrationPassword, registrationEmail: Textbox field ok, cancel: Button constructor AuthenticationDialog() is Create all component objects. Pass "this" to their constructor to register itself as a mediator. method notify(type, sender) is if (type == "check" and sender == loginOrRegister) Show login fields, hide registration fields or vise versa. if (loginOrRegister.checked) title = "Log in" else title = "Register" if (type == "click" and sender == ok) if (loginOrRegister.checked) Try to find user using login credentials. if (!found) Show errors over login fields. else Create account using registration fields. Log user in. // ... // Component classes communicate with mediator using common mediator interface. // Thanks to that, you can use the same components with different mediators. class Component is field parent: Mediator constructor Component(parent) is this.parent = parent method click() is parent.notify("click", this) method keypress() is parent.notify("keypress", this) // Concrete components don't talk with each other. They have only one // communication channel–sending requests to the mediator. class Button extends Component is // ... class Textbox extends Component is // ... class Checkbox extends Component is method check() is parent.notify("check", this) // ...
When chaotic dependencies between components turn a simple change in one component into a series of changes in all components.
Mediator extracts relations between classes into a separate class making changes to one component isolated from the rest of the code.
When you can't reuse a component in a different program because it is too dependent on the other components.
After applying mediator, individual components become unaware of the other components. They communicate indirectly through a mediator object. Reusing a component in a different app requires creating a new mediator class.
When you have to create tons of component subclasses just to use the same components in different contexts.
Mediator encapsulates relations between components. Therefore, it's enough to create a new mediator subclass to define a new set of relations between the same components.
How to Implement
Identify a group of tightly coupled classes that will benefit from being more independent.
Create the Mediator interface and define the communication protocol between the mediator object and various components. In most cases, a single method for receiving notifications from components would be sufficient.
Implement this method inside the Concrete Mediator class. The mediator class will benefit from storing references to all the components that have to be managed.
You can go further and move the component creation code into the mediator. This will turn mediator into a factory.
Components should also store a reference to the mediator. The connection is usually established by passing mediator object as an argument to the component's constructor.
Change the components code so that they call the mediator's notification method when something important happens. Extract the real business logic to the mediator class and execute it when mediator receives a notification from that component.
Pros and Cons
- Reduces coupling between components of a program.
- Allows reusing individual components.
- Centralizes the communications between various components.
- Mediator can evolve into a [сильно раздуться][God Object].
Relations with Other Patterns
Chain of Responsibility passes a request sequentially along a dynamic chain of potential receivers until one of them handles a request.
Observer passes a request to all concerned receivers at the same time but allows them to subscribe and unsubscribe from receiving further requests dynamically.
Command establishes a one-directional connection from senders to receivers.
Mediator has senders and receivers reference each other indirectly.
Mediator is similar to Facade in that it abstracts functionality of existing classes. Mediator abstracts/centralizes arbitrary communications between colleague objects. It routinely "adds value", and it is known/referenced by the colleague objects. In contrast, Facade defines a simpler interface to a subsystem, it doesn't add new functionality, and it is not known by the subsystem classes.
The main goal of the Mediator pattern is to elimintate mutual dependencies between a set of system components. All components become dependent from a single mediator object instead. On the other hand, the goal of the Observer is to establish dynamic one-way connections between objects, where some objects act as subordinates of others.
There's a quite popular Mediator implementation that relies on the Observer pattern. The mediator object acts as publisher and all of the collegue components become subscribers. They dynamically subscribe and unsubscribe to the mediator's events. Implemented this way, both patterns may look very similar.
But the Mediator pattern can be implemented in different way, where all the components are permanently linked to the same mediator object. This implementation won't look as Observer but still be a case of the Mediator pattern.
Now imagine another program where all the components become publishers and allow dynamic connections with each other. There won't be a central mediator object, but rather distribute system of observers.