Mediator pattern defines an object that encapsulates how a set of objects interact.
We generally to create lots of objects interacting one with each other to achieve a good object-oriented design. If the objects interact with each other directly, there is a chance of tight coupling.
The system components are tightly-coupled with each other that make higher maintainability cost and not hard to extend. Mediator pattern helps us to avoid tight-coupling of objects by introducing a layer in between so that the interaction between objects happens via the layer.
Mediator pattern provides a mediator between objects for communication and help in implementing lose-coupling between objects.
Before Mediator Design Pattern
After Mediator Design Pattern
Mediator - defines the interface for communication between Colleague objects.
ConcreteMediator - implements the Mediator interface and coordinates communication and transfer the messages between the colleague objects.
Colleague - defines the interface for communication with other Colleagues
ConcreteColleague - implements the Colleague interface and communicates with
other Colleagues through its Mediator
Mediator Pattern usage in JDK
1. Java Message Service (JMS) uses Mediator pattern along with Observer pattern to allow applications to subscribe and publish data to other applications.
2. java.util.Timer class schedule (i.e scheduleAtFixedRate) methods.
3. Java Concurrency Executor execute() method.
4. java.lang.reflect.Method invoke() method.