Course contentsShow
← Software Engineering
Lesson 299 of 1,9339. Behavioral Design PatternsFree lesson

Mediator vs Observer: Choosing the Right Pattern

Contrast Mediator's centralized control with Observer's distributed notification for different scenarios.

Mediator vs Observer: Choosing the Right Pattern

You'll learn: How to decide between Mediator's centralized control and Observer's distributed notification model based on your system's needs.

The Core Distinction

Both Mediator and Observer manage communication between objects, but they do so in fundamentally different ways:

Mediator acts like a traffic controller at a busy intersection. All communication flows through one central point. Components don't know about each other—they only know the mediator. When something happens, the component tells the mediator, which then decides who else needs to know and orchestrates the response.

Observer works like a newsletter subscription. Publishers (subjects) maintain a list of subscribers (observers) and notify them all when something changes. The communication is direct and distributed—no central authority decides what happens. Each observer independently decides how to react.

When to Choose Mediator

  • Complex, coordinated interactions: When components need to perform intricate workflows together, and you want that logic centralized in one place
  • Tight orchestration: When the mediator needs to control timing, sequencing, or decision-making about who communicates
  • Reducing coupling in many-to-many relationships: When multiple components would otherwise all reference each other

When to Choose Observer

  • Simple notifications: When you just need to broadcast "something happened" without complex coordination
  • Loose coupling: When observers should operate independently and don't need to know about each other
  • Dynamic subscriptions: When observers frequently come and go at runtime
  • One-to-many relationships: When one subject notifies many observers

Key Takeaway: Choose Mediator when you need centralized control and orchestration logic; choose Observer when you need simple, distributed notifications where components react independently to events.