Mediator Pattern

Centralise how a group of objects interact so they no longer refer to each other directly. A runnable Java 21 checkout form where widgets talk only to a mediator, many-to-many versus hub-and-spoke coupling, Mediator versus Observer and Facade, and the god-object risk.

On this page

Simple definition#

The Mediator pattern puts the rules for how a group of objects interact into one object, the mediator. The objects (colleagues) report events to the mediator instead of calling each other; the mediator decides what happens next.

GoF intent: “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.”

The problem#

A checkout form has a billing address field, a “ship to billing address” checkbox, a shipping address field and a submit button:

  • ticking the checkbox copies the billing address and disables the shipping field
  • editing the billing address updates the shipping field if the box is ticked
  • submit is enabled only when both addresses are filled in.

If each widget references the others, the checkbox knows the text fields, the text fields know the button, and so on. With n widgets there can be n × (n − 1) connections. Widgets cannot be reused in another form because they are wired to this one.

Real-world analogy#

An air-traffic control tower. Pilots do not negotiate runways with each other; they talk to the tower, which knows about all aircraft and gives instructions.

Structure#

  • Mediator (FormMediator) — the interface colleagues use to report events.
  • Concrete mediator (CheckoutForm) — knows all colleagues and coordinates them.
  • Colleagues (Checkbox, TextField, Button) — know only the mediator.
Without a mediator (mesh) versus with one (hub)
flowchart LR
    subgraph Mesh["Without: every widget knows the others"]
        B1[billing] <--> C1[checkbox]
        C1 <--> S1[shipping]
        B1 <--> S1
        S1 <--> U1[submit]
        B1 <--> U1
    end
    subgraph Hub["With: widgets know only the mediator"]
        M((CheckoutForm))
        B2[billing] --> M
        C2[checkbox] --> M
        S2[shipping] --> M
        M --> S2
        M --> U2[submit]
    end
Mediator — class diagram of the example
classDiagram
    direction LR
    class FormMediator {
        <<interface>>
        +changed(Widget)
    }
    class CheckoutForm {
        +billing TextField
        +sameAsBilling Checkbox
        +shipping TextField
        +submit Button
        +changed(Widget)
    }
    class Widget {
        <<abstract>>
        #FormMediator mediator
        +setEnabled(boolean)
    }
    class Checkbox
    class TextField
    class Button
    FormMediator <|.. CheckoutForm
    Widget <|-- Checkbox
    Widget <|-- TextField
    Widget <|-- Button
    Widget --> FormMediator : reports to
    CheckoutForm --> Widget : coordinates

Java implementation#

The code is in examples/java/src/hub/patterns/mediator/MediatorDemo.java.

FormMediator View source
/** Mediator: components report events here instead of talking to each other. */
interface FormMediator {
    void changed(Widget source);
}
Colleagues View source
/** Colleague base class: knows only the mediator, never the other widgets. */
abstract class Widget {
    protected final String name;
    protected FormMediator mediator;
    private boolean enabled = true;

    protected Widget(String name) { this.name = name; }

    void attach(FormMediator mediator) { this.mediator = mediator; }
    boolean enabled() { return enabled; }
    void setEnabled(boolean enabled) { this.enabled = enabled; }
}

final class Checkbox extends Widget {
    private boolean checked;

    Checkbox(String name) { super(name); }

    void click() {
        checked = !checked;
        System.out.println("user toggles '" + name + "' -> " + checked);
        mediator.changed(this);
    }
    boolean checked() { return checked; }
}

final class TextField extends Widget {
    private String text = "";

    TextField(String name) { super(name); }

    void type(String value) {
        text = value;
        System.out.println("user types '" + value + "' into " + name);
        mediator.changed(this);
    }
    void setText(String value) { text = value; }
    String text() { return text; }
}

final class Button extends Widget {
    Button(String name) { super(name); }
}
CheckoutForm — concrete mediator View source
/** Concrete mediator: all interaction rules for the checkout form live in one place. */
final class CheckoutForm implements FormMediator {
    final TextField billing = new TextField("billing address");
    final Checkbox sameAsBilling = new Checkbox("ship to billing address");
    final TextField shipping = new TextField("shipping address");
    final Button submit = new Button("Place order");

    CheckoutForm() {
        for (Widget w : new Widget[] {billing, sameAsBilling, shipping, submit}) {
            w.attach(this);
        }
        submit.setEnabled(false);
    }

    @Override public void changed(Widget source) {
        if (source == sameAsBilling || (source == billing && sameAsBilling.checked())) {
            shipping.setEnabled(!sameAsBilling.checked());
            if (sameAsBilling.checked()) {
                shipping.setText(billing.text());
            }
        }
        submit.setEnabled(!billing.text().isBlank() && !shipping.text().isBlank());
        System.out.printf("  shipping=[%s, enabled=%s]  submit enabled=%s%n",
                shipping.text(), shipping.enabled(), submit.enabled());
    }
}
Output View source
user types '1 Main St' into billing address
  shipping=[, enabled=true]  submit enabled=false
user toggles 'ship to billing address' -> true
  shipping=[1 Main St, enabled=false]  submit enabled=true
user types '2 High St' into billing address
  shipping=[2 High St, enabled=false]  submit enabled=true
user toggles 'ship to billing address' -> false
  shipping=[2 High St, enabled=true]  submit enabled=true
user types '9 Dock Rd' into shipping address
  shipping=[9 Dock Rd, enabled=true]  submit enabled=true

All interaction rules are in CheckoutForm.changed(). The widgets are generic and reusable; a different form reuses them with a different mediator.

Mediator versus similar patterns#

PatternWho knows whomCommunication
MediatorColleagues know the mediator; mediator knows colleaguesTwo-way: colleagues report, mediator commands
ObserverSubject knows only an observer interfaceOne-way broadcast; subject does not decide what observers do
FacadeClients know the facade; subsystem does not know itOne-way: facade calls into the subsystem
Event busPublishers and subscribers know only the busBroadcast with no central logic; see the comparison

Mediators are often implemented with Observer: colleagues publish events and the mediator subscribes.

Advantages and disadvantages#

AdvantagesDisadvantages
Replaces many-to-many coupling with one-to-manyThe mediator can become a god object containing all logic
Colleagues are simpler and reusableMediator changes affect every interaction
Interaction logic is in one place: easy to read and changeAdds an indirection when only two objects interact

When to use it#

  • A set of objects communicates in well-defined but complex ways.
  • Reusing an object is hard because it refers to many others.
  • Behaviour distributed across classes should be customisable without subclassing each of them.

When not to use it#

  • Only two or three objects interact simply.
  • Objects only need to broadcast changes without central coordination: use Observer or an event bus.

Common mistakes#

  • Colleagues that still talk to each other, undermining the pattern.
  • Event loops: the mediator updates a colleague, which reports a change, which triggers the mediator again. Guard against re-entrant updates (here setText does not notify).

At architecture scale#

A saga orchestrator in microservices is a mediator: services do not call each other; the orchestrator receives replies and decides the next step. Choreography is the Observer-style alternative where services react to each other’s events. See the order management case study.

Real-world uses#

  • GUI dialogs and controllers (Swing dialogs, JavaFX controllers, MVC controllers).
  • java.util.concurrent.Executor and Timer mediate between tasks and threads.
  • Spring’s ApplicationEventMulticaster dispatches events between beans that do not know each other.
  • MediatR-style command dispatchers and workflow engines (Camunda, Temporal) at a larger scale.

Interview questions and answers#

What is the Mediator pattern? A behavioral pattern that encapsulates how a set of objects interact in a mediator object, so the objects do not reference each other directly.

How does it reduce coupling? It turns a mesh of many-to-many references into a hub: each colleague depends only on the mediator.

What is the main risk? The mediator becoming a god object. Keep one mediator per interaction context and delegate domain logic.

How does Mediator differ from Observer? Observer broadcasts a change to unknown listeners without deciding what they do. A mediator knows its colleagues and actively coordinates them.

Summary#

  • Mediator centralises interaction rules between colleagues.
  • Colleagues know only the mediator, so they become reusable.
  • Watch for god-object mediators and event loops.
  • Saga orchestrators are mediators at architecture scale.

References#

  • Gamma, Helm, Johnson, Vlissides. Design Patterns. Addison-Wesley, 1994. Mediator, p. 273 (the font dialog example).
  • Eric Freeman, Elisabeth Robson. Head First Design Patterns (2nd edition). O’Reilly, 2020. Appendix: Leftover patterns.