The Mediator pattern
The Mediator pattern is another GoF design pattern that controls how objects interact with one another (making it a behavioral pattern). It helps break tight coupling between components of a system by mediating the communication between them.
Goal
The mediator’s role is to manage the communication between objects (colleagues). Those colleagues should not communicate together directly but use the mediator instead.
A mediator is a middleman who relays messages between colleagues.
Design
Let’s start with some UML diagrams. From a very high level, the Mediator pattern is composed of a mediator and colleagues:
Figure 16.3: Class diagram representing the Mediator pattern
When an object in the system wants to send a message to one or more colleagues, it uses the mediator. Here is an example of how it works:
Figure 16.4: Sequence diagram of a mediator relaying messages to colleagues
That is also valid for colleagues...