Bird
0
0
LLDsystem_design~3 mins

Why Mediator pattern in LLD? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if one simple helper could stop all the chaos in your system's communication?

The Scenario

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.

The Problem

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 Solution

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.

Before vs After
Before
class Employee {
  sendMessage(to, message) {
    to.receiveMessage(message);
  }
  receiveMessage(message) {
    console.log(message);
  }
}
After
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);
  }
}
What It Enables

It enables clear, simple, and scalable communication between many parts without chaos or confusion.

Real Life Example

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.

Key Takeaways

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.