Cheatsheet

Mediator

Intent

Put a go-between in the middle of a group of objects that need to coordinate, so each one only talks to it and the rules of how they work together live in one place.

Structure (UML)

Spring contextWodTimerMusicFloorControllerClassFloorFinishLineScreenCoach

Participants

  • Mediator (FloorMediator), the interface with what the devices can report
  • ConcreteMediator (ClassFloor), which knows them all and decides what each does
  • Colleagues (WodTimer and FinishLine report to it; Music and Screen take its orders), which don't know each other. In GoF a colleague often does both; here each device plays only one side

FloorMediator.java

/** The mediator: devices tell it what happened; it decides what everyone else does. */
public interface FloorMediator {
    void timerStarted();
    void athleteFinished(String athlete, int seconds);
    void capReached();
}

Pros

  • No device knows the others, so they can be changed or tested on their own
  • The class rules (when the music plays, what the screen shows) are together
  • Adding a new device touches the mediator, not all the others

Cons

  • The mediator can grow until it knows everything about everyone
  • Following what happens when a button is pressed always means going through it
  • If coordination is minimal, it's one more class with little to do

← Mediator · Run it