The Mediator design pattern is a behavioral design pattern that promotes loose coupling between objects by centralizing communication through a mediator object. This pattern is beneficial when you have a complex system with many interrelated components that need to interact with each other.

The Mediator pattern is beneficial in GUI development, communication systems, and any scenario where multiple objects need to interact in complex ways.

Structure

The Mediator pattern consists of the following key components:

  • Mediator: is an interface that defines methods which are used for communication with Colleague objects
  • Concrete Mediator: are implementations of the Mediator interface. It manages Colleagues and encapsulates the relationship between them
  • Colleague Classes: contains the business logic, and reference to a Mediator. It communicates with the Mediator whenever it wants to communicate with another Colleague
  • Client: uses the Mediator for communication between Colleagues

When to use the Mediator Pattern?

  • When you want to put communication between components into a separate class making it easier to maintain (Single Responsibility Principle)
  • When you want to add new components in the future with ease (Open/Closed Principle)

Implementation Example

The code below demonstrates the implementation of the Mediator design pattern for a simple chat application in Java. It defines a ChatMediator interface and a concrete ChatRoom class that manages communication between users. The User abstract class represents chat participants, with ChatUser as its concrete implementation. Users can send messages through the mediator, which then distributes them to all other users. The Solution class demonstrates the usage, creating a chat room and three users who exchange messages. This structure centralizes communication, reduces direct dependencies between users, and allows for easy addition of new users or features. The pattern simplifies the interaction between objects by having them communicate through the mediator rather than directly with each other.

import java.util.List;
import java.util.ArrayList;

// Client class
public class Solution {

    public static void main(String[] args) {
        
        ChatMediator chatroom = new ChatRoom();

        User john = new ChatUser(chatroom, "John");
        User alice = new ChatUser(chatroom, "Alice");
        User bob = new ChatUser(chatroom, "Bob");

        chatroom.addUser(john);
        chatroom.addUser(alice);
        chatroom.addUser(bob);

        john.send("Hello everyone!");
        System.out.println();
        alice.send("Hi John, how are you?");
        System.out.println();
        bob.send("Hey guys, what's up?");
    }
}

// Mediator interface
interface ChatMediator {

    void sendMessage(String message, User user);
    void addUser(User user);
}

// Concrete Mediator
class ChatRoom implements ChatMediator {

    private List<User> users;

    public ChatRoom() {

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

    public void addUser(User user) {

        this.users.add(user);
    }

    public void sendMessage(String message, User user) {

        for (User u : this.users) {
            
            if (u != user) {
                u.receive(message);
            }
        }
    }
}

// Colleague
abstract class User {

    protected String name;
    protected ChatMediator mediator;

    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);
    }

    public void send(String message) {

        System.out.println(this.name + " sending: " + message);
        mediator.sendMessage(message, this);
    }

    public void receive(String message) {

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

The output of the following program will be:

John sending: Hello everyone!
Alice received: Hello everyone!
Bob received: Hello everyone!

Alice sending: Hi John, how are you?
John received: Hi John, how are you?
Bob received: Hi John, how are you?

Bob sending: Hey guys, what's up?
John received: Hey guys, what's up?
Alice received: Hey guys, what's up?