In this article, we will understand Mediator Design Pattern. This pattern is categorized under behavioral pattern as it can alter the programxs running behavior.
In some cases, the program is made up by using many numbers of classes and these classes need to communicate with each other. In addition to this if these classes are tightly coupled with each other then in it would be a code management issue in long run.
By using the mediator design pattern, communication between objects is encapsulated within a mediator object. Instead of classes communicating directly with each other, classes send messages to the mediator and the mediator send these messages to the other classes. So, in short, this pattern can be used when you see:
- a tight coupling between a set of interacting objects should be avoided.
- an interaction between a set of objects needs to be changed independently.
xHere are ALL other Java Design Patterns, explained in detail with examplesx
Mediator Design Pattern by Example
To understand this pattern we will take an open portal example. In this example portal will act as a mediator and it will share the messages across users in the open chat room.
For illustration, we can have our portal class with a simple static method that will show a message.
OpenPortal Class
public class OpenPortal { public static void showMessage(String message, PortalUser sender) { System.out.println(new Date().toString() + " [" + sender.getName() + "] : " + message); } }
Note above class has a single static method which displays which user has sent the message.
Let us define our portal users:
public class PortalUser { private String name; public PortalUser(String name) { this.name = name; } public void sendMessage(String message) { OpenPortal.showMessage(message, this); } public String getName() { return name; } public void setName(String name) { this.name = name; } }
The user class has a sendMessage method which in turn uses static method exposed by the portal to broadcast message.
Running the Example
public class MediatorDemo { public static void main(String[] args) { PortalUser jack = new PortalUser("Jack"); PortalUser tony = new PortalUser("Tony"); jack.sendMessage("Hi Tony."); tony.sendMessage("Hello Jack, how are you?"); } }
Running this demo is as easy as eating an apple pie 🙂
We have created 2 users and these two users broadcast messages in the chat room.
Output
Sun Mar 25 02:10:54 IST 2018 [Jack] : Hi Tony. Sun Mar 25 02:10:54 IST 2018 [Tony] : Hello Jack, how are you?
Advantages
- Mediator pattern provides maintainable code because here the mediator class which handles all the communications between different classes.
- This pattern supports loose coupling as this reduces the dependencies between communicating objects.
- As it supports loose coupling it is possible to change the interaction between a set of objects independently without affecting the remaining code.
Conclusion
In this article, we understood Mediator Design Pattern with the help of a simple example. The source code is available in our Github repository.
Download Codex
xHere are ALL other Java Design Patterns, explained in detail with examplesx