DEV Community

Iyanu Falaye
Iyanu Falaye

Posted on

Design Patterns: Mediator

Introduction
The Mediator Pattern is a behavioral design pattern that aims to reduce the connectivity between multiple classes or objects by centralizing external communications. Just like its literal meaning, it is a middleman that acts as a broker for communication between two objects. Instead of classes communicating directly with each other, they interact through a mediator object.

Motivation
In a system where components are interconnected, altering one part may have an impact on other parts as well. Direct communication between objects leads to a tightly coupled system, making it hard to manage and maintain. This is handled by the Mediator Pattern, which serves as a central controller, promotes object-to-object communication, and increases system adaptability and maintainability.

Implementation
We are a building chat room application, multiple users can join and start sending messages. A user doesn’t send a message to another user directly but sends it through the chat room which acts as a mediator. The chat room handles the message and forwards it to all other users in the room.

The ChatRoomMediator is the mediator interface that declares a function that shows messages.

public interface ChatRoomMediator {
    void showMessage(User user, String message);
}

Enter fullscreen mode Exit fullscreen mode

ChatRoom is a concrete mediator class that handles communication between user objects. It coordinates users by adding them to a list managed internally and shows each of these users' messages.


public class ChatRoom implements ChatRoomMediator {
    private List<User> users;

    public ChatRoom() {
        this.users = new ArrayList<>();
    }

    public void addUser(User user) {
        this.users.add(user);
    }

    @Override
    public void showMessage(User user, String message) {
        for (User u : users) {
            // message should not be received by the user sending it
            if (u != user) {
                u.receive(message);
            }
        }
    }
}

Enter fullscreen mode Exit fullscreen mode

The User class communicates with other users through the mediator. Each user has a reference to the mediator to send messages, and they receive messages from the mediator.

public class User {
    private String name;
    private ChatRoomMediator chatMediator;

    public User(String name, ChatRoomMediator chatMediator) {
        this.name = name;
        this.chatMediator = chatMediator;
    }

    public void send(String message) {
        chatMediator.showMessage(this, message);
    }

    public void receive(String message) {
        System.out.println(name + " received: " + message);
    }
}
Enter fullscreen mode Exit fullscreen mode

We created a chat room called facebookRoom, created three users Alice, Bob, and Matt, and then added them to Facebook.
they start chatting and this basically shows how the mediator pattern handles the communication between the users by simplifying the process and centralizing external communications.

 ChatRoom facebookRoom = new ChatRoom();

        User alice = new User("Alice", facebookRoom);
        User bob = new User("Bob", facebookRoom);
        User matt = new User("Matt", facebookRoom);


        facebookRoom.addUser(alice);
        facebookRoom.addUser(bob);
        facebookRoom.addUser(matt);

        alice.send("Hi there!");
        bob.send("Hello, Alice!");
        alice.send("Hey bob, where is Matt?");
        matt.send("I am here");
Enter fullscreen mode Exit fullscreen mode

This is what each user received while chatting using the mediator pattern.

Bob received: Hi there!
Matt received: Hi there!
Alice received: Hello, Alice!
Matt received: Hello, Alice!
Bob received: Hey bob, where is Matt?
Matt received: Hey bob, where is Matt?
Alice received: I am here
Bob received: I am here
Enter fullscreen mode Exit fullscreen mode

Advantages:

  1. The pattern reduces the dependency between various classes by centralizing external communications.
  2. It simplifies the system maintenance due to decoupled classes.
  3. It enhances flexibility in terms of object communications and interactions.

Disadvantages:

  1. A mediator can become complex as it centralizes communication between various components.
  2. Components become dependent on the mediator, leading to a single point of failure.

Usecase

A real-world use case of the Mediator Pattern is in a chat room application, multiple users can join and start sending messages. A user doesn’t send a message directly to another user but sends it through the chat room (mediator). The chat room handles the message and forwards it to all other users in the room.

Conclusion

The Mediator Pattern is essential for managing complex communications within a system. It makes interactions simpler and reduces dependencies between system components, promoting a more maintainable and adaptable system. However, careful consideration is necessary to prevent the mediator from becoming a monolithic entity.

Top comments (0)