# Mediator

- Purpose
- Structure
- Interaction
- Applications
- Consequences

## Purpose

- Define an object that encapsulates how a set of objects interact.
- Mediator promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently.

## Structure

- Mediator : defines an interface for communicating with Colleague objects.
- ConcreteMediator : implements cooperative behavior by coordinating Colleague objects. knows and maintains its colleagues.
- Colleague Class : each Colleague class knows its Mediator object. each colleague communicates with its mediator whenever it would have otherwise communicated with another colleague.

## Interaction

- Here's the succession of events by which a list box's selection passes to an entry field.
  1. The list box tells its director that it's changed.
  2. The director gets the selection from the list box.
  3. The director passes the selection to the entry field.
  4. Now that the entry field contains some text, the director enables button(s) for initiating an action

## Applications

- a set of objects communicate in well-defined but complex ways. The resulting interdependencies are unstructured and difficult to understand.
- l reusing an object is difficult because it refers to and communicates with many other objects.
- a behavior that's distributed between several classes should be customizable without a lot of subclassing.

## Consequences

- It limits subclassing. A mediator localizes behavior that otherwise would be distributed among several objects. Changing this behavior requires subclassing Mediator only; Colleague classes can be reused as is.
- It decouples colleagues. A mediator promotes loose coupling between colleagues. You can vary and reuse Colleague and Mediator classes independently.
- It simplifies object protocols. A mediator replaces many-to-many interactions with one-to-many interactions between the mediator and its colleagues. One-to-many relationships are easier to understand, maintain, and extend.
- It abstracts how objects cooperate. Making mediation an independent concept and encapsulating it in an object lets you focus on how objects interact apart from their individual behavior. That can help clarify how objects interact in a system.
- It centralizes control. The Mediator pattern trades complexity of interaction for complexity in the mediator. Because a mediator encapsulates protocols, it can become more complex than any
  individual colleague. This can make the mediator itself a monolith that's hard to maintain.