GofPatterns Gofpatterns





Behavioral Patterns  «Prev  Next»

Mediator Pattern

Description
The Mediator pattern simplifies communication among objects in a system by introducing a single object that manages message distribution among other objects. The Mediator pattern promotes loose coupling by keeping objects from referring to each other explicitly, and it lets you vary their interaction independently. The diagram below illustrates the Mediator pattern.

Mediator Pattern: A single object that manages message distribution among other objects.
Mediator Pattern: A single object that manages message distribution among other objects.

Mediator Pattern Java Code

Benefits:
The following lists the benefits of using the Mediator pattern:
  1. Decouples colleagues
  2. Simplifies object protocols
  3. Centralizes Control
  4. The individual components become simpler and easier to deal with, because they no longer need to directly pass messages to each other
  5. Components are more generic, because they no longer need to contain logic to deal with their communication with other components.
When to Use:
You should use the Mediator pattern when:
  1. A set of objects communicate in well-defined but complex ways.
  2. You want to customize a behavior that's distributed between several objects without using subclasses.

Mediator Pattern consisting of Mediator, ConcreteMediator, Colleague, and ConcreteColleague
Mediator Pattern consisting of Mediator, ConcreteMediator, Colleague, and ConcreteColleague