Implementing the Mediator pattern
The Mediator pattern is another GoF design pattern that controls how objects interact with one another (making it a behavioral pattern).
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. The mediator helps break tight coupling between these colleagues.
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 14.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 14.4: Sequence diagram of a mediator relaying messages to colleagues
That is also valid for colleagues; if they need to talk...