What if one simple helper could stop all the chaos in your system's communication?
Why Mediator pattern in LLD? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine a busy office where every employee tries to talk directly to every other employee to get work done. The phone lines are always busy, messages get lost, and confusion spreads quickly.
When everyone talks directly, communication becomes tangled and hard to manage. It's slow, mistakes happen often, and fixing problems means untangling a big mess of conversations.
The Mediator pattern acts like a smart office manager who handles all communication. Employees talk only to the manager, who then passes messages correctly and keeps everything organized and clear.
class Employee {
sendMessage(to, message) {
to.receiveMessage(message);
}
receiveMessage(message) {
console.log(message);
}
}class Mediator { send(sender, receiver, message) { receiver.receiveMessage(message); } } class Employee { constructor(mediator) { this.mediator = mediator; } sendMessage(to, message) { this.mediator.send(this, to, message); } receiveMessage(message) { console.log(message); } }
It enables clear, simple, and scalable communication between many parts without chaos or confusion.
In a chat app, instead of every user sending messages directly to others, a server (mediator) manages all messages, ensuring they reach the right people smoothly.
Direct communication between many parts causes confusion and errors.
The Mediator pattern centralizes communication to simplify interactions.
This leads to easier maintenance and better scalability.
Practice
Mediator pattern in system design?Solution
Step 1: Understand the role of Mediator
The Mediator pattern acts as a central hub to manage communication between components, avoiding direct links between them.Step 2: Compare options with Mediator's purpose
To centralize communication between components and reduce dependencies correctly states the purpose: centralizing communication and reducing dependencies. Other options describe unrelated or incorrect behaviors.Final Answer:
To centralize communication between components and reduce dependencies -> Option DQuick Check:
Mediator centralizes communication = A [OK]
- Thinking Mediator increases direct component communication
- Confusing Mediator with data storage patterns
- Assuming Mediator merges components into one
Solution
Step 1: Identify typical Mediator method signature
The Mediator usually has a method to notify it about events from components, often with sender and event details.Step 2: Match method signatures to this pattern
interface Mediator { void notify(Component sender, String event); } matches this pattern withnotify(Component sender, String event). Others lack sender info or use incorrect method names.Final Answer:
interface Mediator { void notify(Component sender, String event); } -> Option AQuick Check:
Notify method with sender and event = B [OK]
- Omitting sender parameter in notify method
- Using generic sendMessage without context
- Naming methods incorrectly for Mediator role
class Mediator {
notify(sender, event) {
if (event === 'A') return 'Handled A';
if (event === 'B') return 'Handled B';
return 'Unknown event';
}
}
const mediator = new Mediator();
console.log(mediator.notify('Component1', 'B'));Solution
Step 1: Analyze notify method logic
The method returns 'Handled A' if event is 'A', 'Handled B' if event is 'B', else 'Unknown event'.Step 2: Check the call with event 'B'
The call ismediator.notify('Component1', 'B'), so it matches the second condition and returns 'Handled B'.Final Answer:
Handled B -> Option CQuick Check:
Event 'B' returns 'Handled B' [OK]
- Confusing event 'B' with 'A'
- Assuming default case triggers for known events
- Expecting error due to missing parameters
class Mediator {
notify(sender, event) {
if (event === 'start') {
sender.start();
} else if (event === 'stop') {
sender.stop();
}
}
}
class Component {
start() { console.log('Started'); }
stop() { console.log('Stopped'); }
}
const mediator = new Mediator();
const comp = new Component();
mediator.notify(comp, 'start');Solution
Step 1: Review Mediator's notify method behavior
The Mediator callsstart()orstop()directly on the sender component.Step 2: Identify design issue
This direct call creates tight coupling between Mediator and Component, defeating the purpose of loose coupling in Mediator pattern.Final Answer:
Mediator calls methods on sender directly, creating tight coupling -> Option BQuick Check:
Tight coupling breaks Mediator pattern goal = A [OK]
- Ignoring tight coupling caused by direct calls
- Thinking missing notify in Component is an error
- Assuming constructor absence causes failure
Solution
Step 1: Understand the chat communication needs
Users need a central place to send and receive messages without direct dependencies on each other.Step 2: Match with Mediator pattern usage
A ChatRoom mediator receives messages from users and forwards them to the intended recipients. describes a ChatRoom mediator that manages message routing, fitting the Mediator pattern perfectly.Final Answer:
A ChatRoom mediator receives messages from users and forwards them to the intended recipients. -> Option AQuick Check:
Central message routing = C [OK]
- Choosing direct user-to-user messaging (no mediator)
- Confusing data storage with communication pattern
- Selecting unrelated communication methods
