
to notify the mediator about various events. The mediator interface declares a method used by components Upon receiving a notification about an event, the dialog decides what element should address the event and redirects the call accordingly. It knows how concrete elements are supposed to collaborate and facilitates their indirect communication. In this example, the whole authentication dialog acts as the mediator. Instead, the element only needs to let its mediator know about the event, passing any contextual info along with that notification. In this example, the Mediator pattern helps you eliminate mutual dependencies between various UI classes: buttons, checkboxes and text labels.Īn element, triggered by a user, doesn’t communicate with other elements directly, even if it looks like it’s supposed to. The sender doesn’t know who’ll end up handling its request, and the receiver doesn’t know who sent the request in the first place. When the mediator receives the notification, it can easily identify the sender, which might be just enough to decide what component should be triggered in return.įrom a component’s perspective, it all looks like a total black box. If something important happens within or to a component, it must only notify the mediator. Concrete mediators often keep references to all components they manage and sometimes even manage their lifecycle.Ĭomponents must not be aware of other components. Components may pass any context as arguments of this method, including their own objects, but only in such a way that no coupling occurs between a receiving component and the sender’s class.Ĭoncrete Mediators encapsulate relations between various components. The Mediator interface declares methods of communication with components, which usually include just a single notification method. The component isn’t aware of the actual class of the mediator, so you can reuse the component in other programs by linking it to a different mediator. Each component has a reference to a mediator, declared with the type of the mediator interface. The fewer dependencies a class has, the easier it becomes to modify, extend or reuse that class.Ĭomponents are various classes that contain some business logic. This way, the Mediator pattern lets you encapsulate a complex web of relations between various objects inside a single mediator object. Thus, our submit button should now be able to work with any dialog that implements that interface.

The interface would declare the notification method which all form elements can use to notify the dialog about events happening to those elements. You can go further and make the dependency even looser by extracting the common interface for all types of dialogs. Thus, instead of being tied to a dozen form elements, the button is only dependent on the dialog class. Upon receiving this notification, the dialog itself performs the validations or passes the task to the individual elements.

Now its single job is to notify the dialog about the click. Previously, each time a user clicked the button, it had to validate the values of all individual form elements. The most significant change happens to the actual form elements. UI elements should communicate indirectly, via the mediator object. Most likely, the dialog class is already aware of all of its sub-elements, so you won’t even need to introduce new dependencies into this class. In our example with the profile editing form, the dialog class itself may act as the mediator. As a result, the components depend only on a single mediator class instead of being coupled to dozens of their colleagues. Instead, these components must collaborate indirectly, by calling a special mediator object that redirects the calls to appropriate components. The Mediator pattern suggests that you should cease all direct communication between the components which you want to make independent of each other. You can use either all the classes involved in rendering the profile form, or none at all.

For example, you won’t be able to use that checkbox class inside another form, because it’s coupled to the dog’s text field.
#INDIRECTION DESIGN PATTERN CODE#
Hence, changes to some elements may affect the others.īy having this logic implemented directly inside the code of the form elements you make these elements’ classes much harder to reuse in other forms of the app. Another example is the submit button that has to validate values of all fields before saving the data.Įlements can have lots of relations with other elements. For instance, selecting the “I have a dog” checkbox may reveal a hidden text field for entering the dog’s name. Some of the form elements may interact with others. Relations between elements of the user interface can become chaotic as the application evolves.

It consists of various form controls such as text fields, checkboxes, buttons, etc. Say you have a dialog for creating and editing customer profiles.
