Design Patterns
Last updated Mar 5, 2002
Mediator |
กก |
Object Behavioral Design Pattern |
E.
Gamma, et al., Design Pattern : Elements of Reusable Object-Oriented
Software, Addison-Wesley, 1995, Chapter 5, pp. 273-282
- Intent
- Also Known As
- Motivation
- Applicability
- Structure
- Participants
- Collaborations
- Consequences
- Implementation
- Sample
Code
- Known
Uses
- Related
Patterns
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.
Use the Mediator pattern when
- a set of objects communicate in well-defined but complex ways.
- 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 with a lot of subclassing.

- Mediator
- defines an interface for communicating with Colleague objects.
- ConcreteMediator
- implements cooperative behavior by coordinating Colleague objects.
- knows and maintains its colleagues.
- Colleague classes
- each Colleague class knows its Mediator object.
- each colleague communicates with its mediator whenever it would have
otherwise communicated with another colleague.
- Colleagues send and receive requests from a Mediator object.
- The mediator implements the cooperative behavior by routing requests
between the appropriate colleague(s).
- It limits subclassing.
- The Mediator pattern requires subclassing Mediator only to change
cooperative behavior among several objects.
- Colleague classes can be reused as is.
- It decouples colleagues.
- Loose coupling between colleagues lets 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.
- It abstracts how objects cooperate.
- The Mediator pattern makes coordination an independent concept and
encapsulates it in an object.
- Abstracting object-cooperation this way lets you focus on how objects
interact apart from their individual behavior.
- It centralizes control.
- Because a mediator encapsulates protocols, it can become more complex
than any individual colleague and hard to maintain.
- Omitting the abstract Mediator class.
- The abstract coupling that the Mediator class provides lets colleagues
work with different Mediator subclasses, and vice versa.
- When colleagues work with only one type of Mediator, you don't need to
define an abstract Mediator class.
- Colleague-Mediator communication.
- Colleagues communicate with their mediator when an event of interest
occurs.
- One approach is to implement the Mediator as an Observer using the Observer
pattern.
- Colleagues act as subjects notifying the mediator whenever they change
state.
- The mediator responds by propagating the effects of the change to
other colleagues.
- Facade
abstracts a subsystem of objects to provide a more convenient interface.
- Colleagues can communicate with the mediator using the Observer
pattern.
Taewoong Jeon jeon@korea.ac.kr
Department of Computer
Science, Korea University