GofPatterns Gofpatterns  





Behavioral Patterns  «Prev 

Mediator Pattern: motivation

In order to have a good object oriented design we have to create a lot of classes that interact with each other. If certain principles are not applied, the final framework will end in disarray 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.
The North light must 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 must also tell the south light to provide a walk signal.
It must also tell the south light to provide a walk signal

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