Mediator pattern

It promotes loose coupling by keeping objects from referring to each other explicitly, and it allows their interaction to be varied independently.

Assuming that Colleague1 wants to interact with Colleague2 (to update/synchronize its state, for example), Colleague1 calls mediate(this) on the Mediator1 object, which gets the changed data from Colleague1 and performs an action2() on Colleague2.

Mediator - defines the interface for communication between Colleague objects ConcreteMediator - implements the mediator interface and coordinates communication between Colleague objects.

A chat room could use the mediator pattern, or a system where many ‘clients’ each receive a message each time one of the other clients performs an action (for chat rooms, this would be when each person sends a message).

In reality using the mediator pattern for a chat room would only be practical when used with remoting.

Using raw sockets would not allow for the delegate callbacks (people subscribed to the Mediator class’ MessageReceived event).

When a storage object wants to emit an event indicating that its value has changed, it also goes back to the mediator object (via the method notifyObservers) that controls the list of the observers (implemented using the observer pattern).

A sample UML class and sequence diagram for the mediator design pattern. [ 5 ]
The mediator behavioural design pattern