Java Omnibus
OOAD & Architecture / GoF patterns / Behavioral / Mediator Pattern in Java
GoF Behavioral Java Order Management
GoF Pattern

Mediator helps when many objects collaborate, but direct peer-to-peer coordination is making the system too tangled.

Define an object that encapsulates how a set of objects interact, promoting loose coupling by keeping colleagues from referring to each other directly.

Intent

What the pattern is trying to do

Define an object that encapsulates how a set of objects interact, promoting loose coupling by keeping colleagues from referring to each other directly.

Problem

What force creates the need

Many peer objects coordinate with each other, and the collaboration rules become tangled across the entire object graph.

Solution

How the pattern responds

Move interaction rules into a mediator so colleagues communicate through one coordination point instead of talking to each other directly.

Structure

How the moving parts fit together

Colleagues notify a mediator, and the mediator decides how the collaboration should proceed across the participating objects.

Java Example

Java example in the Order Management domain

Mediator Pattern in Java example
package org.javaomnibus.ecommerce.gof.behavioral;

public final class CheckoutMediator {
    private final InventoryService inventoryService;
    private final PaymentService paymentService;
    private final NotificationService notificationService;

    public CheckoutMediator(
        InventoryService inventoryService,
        PaymentService paymentService,
        NotificationService notificationService
    ) {
        this.inventoryService = inventoryService;
        this.paymentService = paymentService;
        this.notificationService = notificationService;
    }

    public void finalizeOrder(Order order) {
        inventoryService.reserve(order);
        paymentService.capture(order);
        notificationService.sendConfirmation(order);
    }
}
Code Walkthrough

Step by step

  1. Collaboration rules live in one coordinating object.
  2. Participants stay less coupled to each other directly.
  3. This is helpful when collaboration complexity is the force, not merely workflow sequence.
  4. Mediator often appears in UI logic, orchestration, and subsystem coordination.
When To Use

Where this pattern helps

  • When many collaborators are tightly interconnected
  • When coordination logic should be centralized for clarity
When Not To Use

When a simpler design is better

  • When a simple direct relationship is clearer
  • When the mediator would just become a god object with too many responsibilities
Trade-Offs

What this pattern costs

  • A mediator can centralize too much and become bloated
  • Hiding direct relationships can make the system’s true dynamics less obvious
Modern Relevance

How this fits today

Mediator remains useful in Java UI flows, orchestration modules, and collaboration-heavy application services where peer coupling otherwise grows quickly.

Common Misuse

Where teams get it wrong

Turning every service coordinator into a mediator can conceal an oversized service object rather than improve collaboration boundaries.

Related Patterns

Compare with nearby choices


What To Read Next