BEHAVIORAL PATTERNS / OBJECT DESIGN BRIEF

Mediator pattern

The Mediator pattern centralizes communication between objects so they do not refer to each other directly.

IntermediatePhase 06 / Topic 8 of 10ResponsibilitiesCollaborationsExtensibility
01

Overview

The Mediator pattern centralizes communication between objects so they do not refer to each other directly. Instead of every component knowing every other component (n x n links), each talks to the mediator, which coordinates them. Chat rooms, air traffic control, UI dialogs, and elevator dispatchers are classic examples.

Mediators reduce coupling and make interactions easier to change in one place. The risk is the mediator becoming a god object that knows everything; keep it focused on coordination, with business rules in the colleagues.

Air traffic control

Pilots do not negotiate directly with every other plane about runways. They talk to the tower, which coordinates who lands and takes off. Adding a plane does not require it to know all the others.

02

When to use it

  • Many objects interact in complex, tangled ways.
  • Components should be reusable without knowing each other.
  • Coordination logic changes more often than the components.
03

Where it shows up in interviews

Coordinating peers

Recognize it when: many objects must react to each other.

  • Design a chat room
  • Design an elevator dispatcher
  • Design an auction system
UI dialogs

Recognize it when: form fields enable/disable each other.

  • Design a flight booking form
  • Design a settings panel
04

Where it is used in real software

Chat servers

Slack and Discord servers route messages between clients; clients never connect to each other directly.

Elevator group controllers

A dispatcher assigns hall calls to cars instead of cars negotiating.

Event buses

In-app event buses (MediatR in .NET) mediate between handlers.

05

Key terms

Mediator
Object that coordinates colleagues.
Colleague
Component that talks only to the mediator.
Many-to-many to one-to-many
The coupling reduction mediators provide.
06

How it works, step by step

  1. 1
    Identify interacting components

    Users in a chat, cars in a building.

  2. 2
    Define the mediator interface

    send(message, from), requestElevator(floor).

  3. 3
    Colleagues hold a mediator reference

    Not references to each other.

  4. 4
    Mediator implements coordination rules

    Routing, assignment, enabling.

  5. 5
    Keep rules small

    Split into several mediators if it grows.

07

Coupling with and without a mediator

10 components that interact

Step 1 / 2
DesignReferencesAdding an 11th component
Direct peer referencesUp to 10 x 9 = 90Touch up to 10 classes
Mediator10 (each to mediator)Register with mediator

NOWDesign: Direct peer references | References: Up to 10 x 9 = 90 | Adding an 11th component: Touch up to 10 classes

Coordination moves to one place; components become independent.

08

Implementation

interface ChatMediator {  join(user: ChatUser): void;  send(message: string, from: ChatUser, to?: string): void;} class ChatRoom implements ChatMediator {  private users = new Map<string, ChatUser>();  join(user: ChatUser) { this.users.set(user.name, user); }  send(message: string, from: ChatUser, to?: string) {    if (to) return this.users.get(to)?.receive(message, from.name);  // direct message    for (const u of this.users.values()) if (u !== from) u.receive(message, from.name); // broadcast  }} class ChatUser {  readonly inbox: string[] = [];  constructor(readonly name: string, private room: ChatMediator) { room.join(this); }  say(message: string, to?: string) { this.room.send(message, this, to); }  receive(message: string, from: string) { this.inbox.push(`${from}: ${message}`); }} const room = new ChatRoom();const ana = new ChatUser("ana", room);const bo = new ChatUser("bo", room);ana.say("hi all");bo.say("hi ana", "ana");
09

Complexity and performance

ReferencesO(n) vs O(n^2)

Coupling reduction.

Routing costDepends on mediator logic

Often O(n).

10

Trade-offs

Decoupling vs god object

The mediator can accumulate all logic; keep domain rules in colleagues.

Indirection

Interactions are less obvious from reading a colleague's code.

11

Variants and related techniques

Mediator vs Observer

Observer broadcasts to subscribers; mediator actively coordinates specific interactions.

Event bus

A generic mediator based on published events.

12

Common mistakes

  • Mediator knowing all internal details of colleagues.

    Fix: Communicate through small interfaces.

  • Colleagues bypassing the mediator.

    Fix: Enforce that peers never reference each other.

13

Interview questions

Mediator vs Facade?

A facade simplifies access to a subsystem for outside clients, and subsystem classes do not know the facade. A mediator coordinates peers that do know the mediator and communicate through it.

How would an elevator system use a mediator?

A dispatcher receives hall calls and assigns them to cars based on position and direction; buttons and cars only talk to the dispatcher, so scheduling policies can change in one place.

14

Practice problems

ProblemDifficultyWhat it trains
Chat room with direct and broadcast messagesEasyRouting.
Elevator dispatcher with strategiesMediumMediator + Strategy.