Mediator Design Pattern In Java
Join the DZone community and get the full member experience.Join For Free
Let's finish some remaining design patterns to cover this topic. Today, I will discuss another design pattern - Mediator Design Pattern.
Mediator Design Pattern
- The Mediator Design Pattern is one of the Gang of Four design patterns which is used to control and reduce complexity of communication among groups of objects.
- The Mediator Pattern is a behavioral pattern which defines an object to encapsulate the interaction between objects.
- The Mediator Pattern helps us to reduce chaotic dependencies between objects.
- The Mediator pattern helps us to restrict any direct communication between the objects and allow us to collaborate objects only via a mediator object.
- By using Mediator object we can control communication between objects.
- The Mediator object also hides the complexity of communication between objects.
- The Mediator pattern defines a mediator class to handle all the communications between different classes. That also supports easy maintenance of the communication related code by using loose coupling.
- The Chatting Application is a good example of Mediator pattern where a group of chat users communicates with each other via the char server.
- The Whatsapp is another good example of the Mediator pattern. Whatsapp server handles all the communication between different whatsapp users.
- Air Traffic Control System is also another great example in which all flights doing takeoff or landing communicates with the Air Traffic Control to manage their safe and smooth movement.
Let's take an example of communication between different workers; each one is assigned to a dedicated type of job to perform. We will see how they can communicate with others with the help of manager for the jobs they are not doing. I will not use multi-threading or network related code just to make the example as simple as possible.
Worker Communication System using Mediator Design Pattern
First, let's define some type of calculation work which we dedicate to each of the worker to perform.
Here's the code for Calculate abstract class:
Here's the code for CalculateArmstrong class:
Here's the code for CalculateFactorial class:
Here's the code for CalculatePalindrome class:
Now, let's define a class called Work to create calculation work which can assign to a Worker:
And, here's the code for Worker class:
Here's the code for Manager class:
Here, I am using a CalculationHub, which will keep the record of calculation and the worker who is dedicated to do that under the same manager (mediator).
Now, let's write a Main class to execute and test the output:
As we can see, we are assigning different type of work to each worker. But, since our worker is dedicated to a single specific calculation job only, he/she will pass the other type of calculation to its manager with w request to assign that to the suitable worker under him.
The Manager, who is like a mediator for us in this example, will use the calculation-hub to identify the suitable worker for the calculation job and re-assign that to him.
So the advantage is each worker knows what he/she can do and does that without any manager's/mediator's involvement. For other type of calculation jobs, he is passing job to the manager to re-assign to the right worker to perform. Worker does not communicate directly each other and are also unaware of each other.
The only way of this controlled communication is via manager and hence manager hides and encapsulates all communication related complexity under him. Our worker is free from all this and can have only code fr his dedicated work plus standard communication to the manager/mediator.
Below is the output of the program:
The Source Code can be found here: Mediator-Design-Pattern-Sample-Code
I hope this tutorial helps in understanding of the mediator design pattern.
Liked the article? Please don't forget to press that like button. Happy coding!
Need more articles, please visit my profile: Brijesh Saxena
Opinions expressed by DZone contributors are their own.