What is a mediator in Java?

Dennis 25 Published: 09/16/2024

What is a mediator in Java?

I apologize for the confusion earlier!

In Java, a mediator is an object that acts as an intermediary between two or more objects in order to facilitate communication and coordination among them. The Mediator design pattern, also known as the "Command Pattern," allows you to decouple objects that would otherwise be tightly coupled, promoting loose coupling and better scalability.

To illustrate this concept, let's consider a simple example: a chat room with multiple users. In a traditional approach, each user would need to have direct references to all other users in order to send messages. This would result in a complex web of interdependencies between users. Instead, we can introduce a Mediator (the Chat Room) that handles the communication between users.

The Chat Room Mediator contains methods for sending and receiving messages, as well as maintaining a list of connected users. When a user sends a message, it gets sent to the Chat Room Mediator, which then broadcasts the message to all other connected users.

Here's a simplified representation of this design:

public interface User {

void receiveMessage(String message);

}

public class ChatRoom implements Mediator {

private List users = new ArrayList<>();

public void addUser(User user) {

users.add(user);

}

public void sendMessage(String message, User sender) {

for (User user : users) {

if (user != sender) {

user.receiveMessage(message);

}

}

}

}

public class ConcreteUser implements User {

private ChatRoom mediator;

public ConcreteUser(ChatRoom mediator) {

this.mediator = mediator;

mediator.addUser(this);

}

public void sendMessage(String message) {

mediator.sendMessage(message, this);

}

public void receiveMessage(String message) {

System.out.println("Received message: " + message);

}

}

By using a Mediator to coordinate communication between objects, you can:

Decouple objects that would otherwise be tightly coupled. Promote loose coupling and better scalability. Reduce the complexity of inter-object dependencies.

In summary, a mediator in Java is an object that acts as an intermediary between two or more objects to facilitate communication and coordination among them, allowing for loose coupling and improved scalability.

Java mediator tutorial

Here is a step-by-step tutorial on implementing the Mediator design pattern in Java:

What is the Mediator Pattern?

The Mediator pattern is a behavioral design pattern that lets you reduce complex direct communications between objects and replace them with only one mediator object that handles these communications.

In simpler terms, imagine you are at a party with many friends. Normally, each friend would talk directly to every other friend they want to communicate with, which could lead to chaos and confusion. With the Mediator pattern, you introduce an event coordinator who acts as a middleman between friends, ensuring that only one person talks at a time and preventing unnecessary conversations.

Java Implementation

Let's create a simple chat room application using Java where multiple users can communicate without knowing each other's details.

First, define the User class, which represents individual users:

public interface User {

void receiveMessage(String message);

}

public class ConcreteUser implements User {

private String name;

public ConcreteUser(String name) {

this.name = name;

}

@Override

public void receiveMessage(String message) {

System.out.println(name + " received message: " + message);

}

}

Next, define the ChatMediator interface:

public interface ChatMediator {

void sendMessage(User user, String message);

}

Now, implement the ChatMediator concrete class, which will act as the mediator between users:

public class ConcreteChatMediator implements ChatMediator {

private List users;

public ConcreteChatMediator() {

this.users = new ArrayList<>();

}

@Override

public void sendMessage(User user, String message) {

for (User u : users) {

if (!u.equals(user)) {

u.receiveMessage(message);

}

}

}

public void addUser(User user) {

users.add(user);

}

}

Finally, create a main class Main that sets up the mediator and allows users to send messages:

public class Main {

public static void main(String[] args) {

ConcreteChatMediator mediator = new ConcreteChatMediator();

User user1 = new ConcreteUser("Alice");

User user2 = new ConcreteUser("Bob");

mediator.addUser(user1);

mediator.addUser(user2);

user1.receiveMessage("Hello"); // Mediator will broadcast this message to all users

user2.receiveMessage("Hi"); // Mediator will also broadcast this message

}

}

In the Main class, we create a mediator object and add two users, Alice and Bob. We then have each user send a message, which is received by all other users through the mediator.

Conclusion

The Mediator pattern helps reduce coupling between objects and promotes loose coupling in your system. By using a mediator to handle communication between objects, you can decouple objects and make them more reusable and easier to maintain. This tutorial provides a basic example of implementing the Mediator pattern in Java, which can be expanded upon in more complex scenarios.

I hope this helps! Let me know if you have any questions or need further clarification.