Mediator Pattern
AdvancedDefines an object that encapsulates how a set of objects interact, promoting loose coupling by preventing direct references between them.
Overview
Without a Mediator, N objects communicating with each other require up to N×(N-1) references — an O(N²) dependency web. The Mediator centralizes communication: all objects talk only to the Mediator, which coordinates and routes messages. This reduces coupling from O(N²) to O(N). The trade-off is that the Mediator itself can become a "god object" if it grows too large. Classic examples: air traffic control tower (planes communicate through tower), chat room (users send messages through chat room), GUI event bus (form fields notify the form via mediator rather than each other).
Chat Room Mediator
A chat room where users do not reference each other. All messages go through the ChatRoom mediator, which routes them to the correct recipients.
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
// Mediator interface
public interface ChatMediator {
void register(User user);
void sendMessage(String message, String fromUserId, String toUserId); // DM
void broadcast(String message, String fromUserId); // group
}
// Colleague
public class User {
private final String id;
private final String name;
private final ChatMediator mediator;
public User(String id, String name, ChatMediator mediator) {
this.id = id;
this.name = name;
this.mediator = mediator;
mediator.register(this); // self-register
}
public String getId() { return id; }
public String getName() { return name; }
public void send(String message, String toUserId) {
mediator.sendMessage(message, this.id, toUserId);
}
public void broadcast(String message) {
mediator.broadcast(message, this.id);
}
// Called by mediator when a message is delivered here
public void receive(String message, String fromName) {
System.out.printf("[%s] ← %s: %s%n", this.name, fromName, message);
}
}
// Concrete Mediator — ChatRoom
public class ChatRoom implements ChatMediator {
private final Map<String, User> users = new ConcurrentHashMap<>();
@Override
public void register(User user) {
users.put(user.getId(), user);
System.out.println(user.getName() + " joined the chat");
}
@Override
public void sendMessage(String message, String fromId, String toId) {
User sender = users.get(fromId);
User recipient = users.get(toId);
if (sender == null || recipient == null) return;
recipient.receive(message, sender.getName()); // mediator routes
}
@Override
public void broadcast(String message, String fromId) {
User sender = users.get(fromId);
if (sender == null) return;
users.values().stream()
.filter(u -> !u.getId().equals(fromId))
.forEach(u -> u.receive(message, sender.getName()));
}
}
// Usage — users never hold references to each other
ChatMediator room = new ChatRoom();
User alice = new User("u1", "Alice", room);
User bob = new User("u2", "Bob", room);
User carol = new User("u3", "Carol", room);
alice.send("Hey Bob, saw the LLD course?", "u2"); // DM to Bob
bob.broadcast("Everyone check aicancode.org!"); // to Alice + CarolKey Points to Remember
- 1Mediator reduces O(N²) peer-to-peer references to O(N) hub-and-spoke references.
- 2Colleagues hold only a reference to the Mediator, never to each other.
- 3The Mediator can become a "god object" anti-pattern if it takes on too much logic — keep it thin.
- 4MediatR (C#) and Spring's ApplicationEventPublisher are Mediator implementations.
- 5Difference from Facade: Facade simplifies a subsystem for external clients; Mediator coordinates objects within a subsystem.
Interview Questions
Sign in to ask AriaHow does Mediator differ from Facade pattern?
What is the risk of the Mediator becoming a "god object"?
How does an air traffic control system demonstrate Mediator pattern?
Compare Mediator and Observer patterns — when would you choose each?
Ask Aria about Mediator Pattern
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.