Design Pattern | Mediator Pattern

What Is the Mediator Pattern?

  • Mediator means an intermediary.
  • If there are 10 members and they all give instructions to each other, work becomes very chaotic. In that situation, if there is an intermediary in a different position, members can report only to the intermediary and receive instructions only from the intermediary.
  • The Mediator pattern causes behavior to happen through a mediator.
  • In GoF design patterns, it is classified as a behavioral design pattern.

Mediator Pattern Example Program

This example program displays a login dialog and controls the enabled or disabled state of text fields and buttons.

Class Diagram
Mediator Pattern Class Diagram

The example defines a Mediator interface, colleague controls, concrete UI widgets, and a concrete mediator that coordinates the widgets. Instead of widgets controlling each other directly, they notify the mediator when their state changes, and the mediator decides the overall UI state.

Advantages

The Mediator pattern reduces complex many-to-many relationships between objects. Each colleague only knows the mediator, making interaction rules easier to centralize and change.