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.
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.
What force creates the need
Many peer objects coordinate with each other, and the collaboration rules become tangled across the entire object graph.
How the pattern responds
Move interaction rules into a mediator so colleagues communicate through one coordination point instead of talking to each other directly.
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 in the Order Management domain
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);
}
}
Step by step
- Collaboration rules live in one coordinating object.
- Participants stay less coupled to each other directly.
- This is helpful when collaboration complexity is the force, not merely workflow sequence.
- Mediator often appears in UI logic, orchestration, and subsystem coordination.
Where this pattern helps
- When many collaborators are tightly interconnected
- When coordination logic should be centralized for clarity
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
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
How this fits today
Mediator remains useful in Java UI flows, orchestration modules, and collaboration-heavy application services where peer coupling otherwise grows quickly.
Where teams get it wrong
Turning every service coordinator into a mediator can conceal an oversized service object rather than improve collaboration boundaries.