SOLID & Design Patterns Interview Question #40

What is the difference between Mediator and Observer?

Structural, Creational & Behavioral Patterns Junior Beginner

Quick Interview Answer

Mediator coordinates interactions through a central collaboration object. Observer broadcasts notifications from a subject to interested subscribers, usually without the publisher coordinating a complete multi-object workflow.

Detailed Explanation

Mediator coordinates interactions through a central collaboration object. Observer broadcasts notifications from a subject to interested subscribers, usually without the publisher coordinating a complete multi-object workflow.

Mediator is useful when several components would otherwise know too much about each other. Observer is useful when multiple independent listeners need to react to an event.

Code Example

// Mediator: request/response coordination
var order = await mediator.SendAsync(
    new GetOrderQuery(id));

// Observer: event notification
orderService.OrderCreated += OnOrderCreated;