Exploring the Mediator Design Pattern in Java: Implementation and Use Cases

Hey 👋🏻, I am , a Software Engineer from India. I am interested in, write about, and develop (open source) software solutions for and with JavaScript, ReactJs. 📬 Get in touch
Twitter: https://x.com/SankalpHaritash Blog: https://sankalp-haritash.hashnode.dev/ LinkedIn: https://www.linkedin.com/in/sankalp-haritash/ GitHub: https://github.com/SankalpHaritash21
📧 Sign up for my newsletter: https://sankalp-haritash.hashnode.dev/newsletter
Welcome to our guide on the Mediator design pattern in Java! In this tutorial, we'll delve into the Mediator pattern, its implementation in Java, and explore its various use cases. By the end, you'll have a solid understanding of how to leverage the Mediator pattern to facilitate communication between objects effectively in your Java applications.
Understanding the Mediator Design Pattern:
The Mediator pattern is a behavioral design pattern that promotes loose coupling by centralizing communication between objects. It defines an object (the Mediator) that encapsulates how objects interact, thus reducing dependencies between them. Instead of objects communicating directly, they communicate through the Mediator, which facilitates their interactions.
Implementation in Java: Let's implement the Mediator pattern in Java using a simple chat application scenario, where users communicate through a chat room.
// Define Mediator interface
interface ChatMediator {
void sendMessage(String message, User user);
void addUser(User user);
}
// Concrete Mediator
class ChatRoom implements ChatMediator {
@Override
public void sendMessage(String message, User user) {
System.out.println(user.getName() + " sends message: " + message);
}
@Override
public void addUser(User user) {
System.out.println(user.getName() + " joined the chat");
}
}
// Colleague interface
abstract class User {
protected ChatMediator mediator;
protected String name;
public User(ChatMediator mediator, String name) {
this.mediator = mediator;
this.name = name;
}
public abstract void send(String message);
public abstract void receive(String message);
}
// Concrete Colleague
class ChatUser extends User {
public ChatUser(ChatMediator mediator, String name) {
super(mediator, name);
}
@Override
public void send(String message) {
System.out.println(name + " sends: " + message);
mediator.sendMessage(message, this);
}
@Override
public void receive(String message) {
System.out.println(name + " receives: " + message);
}
}
// Usage
public class Main {
public static void main(String[] args) {
ChatMediator chatMediator = new ChatRoom();
User user1 = new ChatUser(chatMediator, "User1");
User user2 = new ChatUser(chatMediator, "User2");
chatMediator.addUser(user1);
chatMediator.addUser(user2);
user1.send("Hello, everyone!");
user2.send("Hi there!");
}
}
Benefits of the Mediator Pattern:
1. Decoupling: The Mediator pattern decouples the objects by centralizing communication between them. Objects no longer need to maintain references to each other, reducing dependencies.
2. Simplified Communication: Objects communicate through a single interface provided by the Mediator, making the communication process more straightforward and easier to manage.
3. Centralized Control: The Mediator encapsulates the communication logic, providing centralized control over interactions between objects. This simplifies the maintenance and modification of communication behavior.
4. Reusability: As the communication logic is encapsulated within the Mediator, it can be reused across different sets of objects, promoting code reusability.
Conclusion: The Mediator design pattern is a valuable tool for facilitating communication between objects while promoting loose coupling and encapsulation. By centralizing communication logic in a mediator object, the Mediator pattern enhances code maintainability, flexibility, and reusability. Whether you're building chat applications, GUI frameworks, or distributed systems, understanding and applying the Mediator pattern can greatly improve the design and scalability of your Java applications.
Conclusion:
If you found this blog post helpful, please consider sharing it with others who might benefit. Follow me for more insightful content on JavaScript, System Design, and other web development topics.
Connect with me on Twitter, LinkedIn, and GitHub for updates and more discussions.
Thank you for reading! 😊





