Card 02/ 08
Mediator Pattern Structure: The Class and Sequence Diagrams
Colleagues know only the Mediator, and the Mediator knows them all.
Mediator has four participants: a mediator interface and its concrete class, and the colleagues and their concrete classes. This card reads their roles off the Gang of Four's class diagram, then follows one change through the mediator in a sequence diagram, so you can see that no colleague ever calls another.
| Participant | Role |
|---|---|
| Mediator | Declares how colleagues report events, often one notify(sender, event) method. |
| ConcreteMediator | Holds every colleague, and decides what each event means for the others. |
| Colleague | Holds the mediator and reports its events to it. |
| ConcreteColleague | Does its own job, and calls the mediator instead of another colleague. |
The arrows run in both directions, but not symmetrically. Each colleague knows only the Mediator interface, so a colleague can be reused with a different mediator. The concrete mediator knows every concrete colleague, because deciding what an event means for the others requires knowing who they are.
The sequence diagram follows one change: colleague A reports an event, and the mediator updates B and C. A never learns who reacted.
A mediator is sometimes confused with a facade, which also stands in front of several objects. The difference is direction. A facade is called by clients from outside, and the objects behind it do not know it exists. A mediator is called by the objects it coordinates, and they all depend on it.
← → move through sections and cards