What if one simple helper could stop all the chaos in your system's communication?
Why Mediator pattern in LLD? - Purpose & Use Cases
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.
