GofPatterns Gofpatterns  





Behavioral Patterns  «Prev 

Mediator Pattern: Motivation

In order to have good object oriented design, we have to create many classes that interact with each other. If certain principles are not applied, the final framework will be incohesive, where each object relies on many other objects in order to perform their function. In order to avoid tightly coupled frameworks, we need a mechanism to facilitate the interaction between objects so that objects are not aware of the existence of other objects.
Let us take the example of a user interface for an application.
When we create the GUI, we add all sort of controls to the screen. One control needs to interact with all the other controls. For example when a button is pressed, it must know if the data is valid in other controls.
If you created different applications using forms, you do not have to modify each control class each time you add a new control to the form. All the operations between controls are managed by the form class itself and this is known as the mediator pattern.


The North light must synchronize with the other lights.
Suppose the pedestrian wants to cross the street and pushes the button on the North light. The north light must now synchronize with the other lights

It has to tell the east and west lights to turn yellow, then red
It has to tell the east and west lights to turn yellow, then red. It also has to wait until they respond that they have in fact done so.


It must also tell the south light to provide a walk signal.
It must also tell the south light to provide a walk signal. The south light must also respond.

Every change in any light requires all four lights to go through a complicated sequence of request and response.
Every change in any light requires all four lights to go through a complicated sequence of request and response