GofPatterns Gofpatterns  





Behavioral Patterns  «Prev  Next»
Lesson 8 Mediator: motivation
ObjectiveExamine Problems that Require the Mediator Pattern.

Examine Problems that Require the Mediator Pattern

Out of all the design patterns, the Mediator pattern has probably been reinvented more times than any other. It is an absolutely essential pattern any time you have many different components of a system which have to maintain a consistent state as a whole. It is used by any system in which many different parts must work together efficiently while sharing the same basic information.
We are going to use the Mediator pattern to manage the traffic lights in our traffic system. The SlideShow below illustrates the motivation of the Mediator pattern specific to our project:


Alternative Images to the SlideShow.
Traffic Light Mediator Gallery
As you add more complicated intersections with turn signals, turning lanes, and more, the complexity grows. You need a way of untangling all these connections. The Mediator pattern provides it.


In general, object-oriented applications consist of a set of objects that interact with each other for the purpose of providing a service. This interaction can be direct (point-to-point) as long as the number of objects referring to each other directly is very low. Figure 1 depicts this type of direct interaction where ObjectA and ObjectB refer to each other directly.
Figure 1: Point to Point Communication in  the case of two objects
Figure 1: Point to Point Communication in the case of two objects

As the number of objects increases, this type of direct interaction can lead to a complex maze of references among objects (Figure 2), which affects the maintainability of the application. Also, having an object directly referring to other objects greatly reduces the scope for reusing these objects because of higher coupling.
Figure 2: Point-to-Point Communication with an increased number of objects.
Figure 2: Point-to-Point Communication with an increased number of objects.