The Mediator Pattern is a behavioral design pattern that centralizes communication between multiple objects by introducing a mediator object.

Instead of objects communicating directly with one another, they interact through the mediator. This reduces dependencies between objects and prevents a complex network of inter-object references.

The mediator coordinates interactions, allowing individual objects to focus only on their own responsibilities without knowing about other participants.

The Mediator Pattern is commonly used in chat applications, air traffic control systems, GUI frameworks, workflow orchestration, and event coordination.

Structure

The Mediator Pattern consists of the following participants.

1. The Mediator defines the interface for communication between colleague objects.
2. The Concrete Mediator coordinates interactions among colleagues.
3. The Colleagues communicate only with the mediator instead of communicating directly with each other.
4. The Client creates the mediator and associates it with the colleagues.

Java Implementation

Suppose an application implements a simple chat room. Users should send messages through the chat room instead of communicating directly with one another.

Create the mediator interface.
public interface ChatMediator {
    void sendMessage(String message, User sender);
}
Create the concrete mediator.
import java.util.ArrayList;
import java.util.List;

public class ChatRoom implements ChatMediator {
    private final List<User> users = new ArrayList<>();

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

    @Override
    public void sendMessage(String message, User sender) {
        for (User user : users) {
            if (user != sender) {
                user.receive(message);
            }
        }
    }
}
Create the colleague.
public class User {
    private final String name;
    private final ChatMediator mediator;

    public User(String name, ChatMediator mediator) {
        this.name = name;
        this.mediator = mediator;
    }

    public void send(String message) {
        mediator.sendMessage(message, this);
    }

    public void receive(String message) {
        System.out.println(name + " received: " + message);
    }
}
The client communicates through the mediator.
public class Main {
    public static void main(String[] args) {
        ChatRoom chatRoom = new ChatRoom();

        User alice = new User("Alice", chatRoom);
        User bob = new User("Bob", chatRoom);

        chatRoom.addUser(alice);
        chatRoom.addUser(bob);

        alice.send("Hello Bob!");
    }
}
Output:
Bob received: Hello Bob!

How It Works?

The client creates a ChatRoom, which acts as the mediator responsible for coordinating communication between users.

Each User maintains a reference only to the mediator and never communicates directly with another user.

When Alice sends a message, the request is forwarded to the ChatRoom. The mediator determines the recipients and delivers the message by invoking their receive() methods.

Because all communication flows through the mediator, new users or additional communication rules can be introduced without modifying the existing user classes.

Advantages

1. It reduces direct dependencies between collaborating objects.
2. Communication logic is centralized in a single mediator.
3. Colleague objects become simpler and more focused.
4. New colleagues can be added with minimal changes to existing code.

Disadvantages

1. The mediator can become large and complex if it coordinates too many objects.
2. Centralizing communication may introduce a single point of failure.
3. Additional indirection can make debugging interactions more difficult.

JDK Examples

The Mediator Pattern appears throughout the Java ecosystem.

1. The java.awt.Dialog and related Swing components coordinate interactions between multiple UI controls, reducing direct communication among them.

2. The java.beans.PropertyChangeSupport class acts as a central coordinator for property change notifications between objects.

Summary

The Mediator Pattern centralizes communication between collaborating objects by introducing a mediator that coordinates their interactions.

By removing direct dependencies between objects, the pattern promotes loose coupling, simplifies collaboration, and makes complex interactions easier to manage and extend.
Nagesh Chauhan

Nagesh Chauhan

Principal Software Engineer • Java • Python • Distributed Systems • AI/ML

Principal Software Engineer with 14+ years of experience designing and delivering large-scale distributed systems, cloud-native applications, and AI-powered platforms.

Passionate about solving complex engineering problems using strong data structures and algorithms, along with expertise in Java, Spring Boot, Python, System Design, Microservices, Cloud, Kafka, Elasticsearch, and Generative AI.

Share this Article

💬 Comments

Join the Discussion