JavaOmnibus
GoF Mediator
GoF pattern • Behavioral
GoFBehavioralJava example

Mediator solves a specific structural problem in the design.

Centralize complex interactions between collaborators.

Pattern overview

GoF pattern

Centralize complex interactions between collaborators.

Intent

Define an object that encapsulates how a set of objects interact.

Problem

Peer-to-peer communication among many components becomes tangled and hard to change.

Solution

Move collaboration rules into a mediator that coordinates participants.

Fit and tradeoffs

FamilyGoF
CategoryBehavioral
Best used when
  • When UI components, workflows, or modules have dense interaction rules.
Tradeoffs
  • The mediator can become too large if it owns too much policy.

Java example

This example is intentionally compact so the pattern shape stays easy to see.

interface CheckoutMediator {
    void itemAdded(String sku);
}

final class CartCheckoutMediator implements CheckoutMediator {
    private final PricingPanel pricingPanel;
    private final RecommendationPanel recommendationPanel;

    CartCheckoutMediator(PricingPanel pricingPanel, RecommendationPanel recommendationPanel) {
        this.pricingPanel = pricingPanel;
        this.recommendationPanel = recommendationPanel;
    }

    public void itemAdded(String sku) {
        pricingPanel.refreshTotals();
        recommendationPanel.refreshFor(sku);
    }
}

Related pages

Keep exploring