Notification fan-out
Observer is best when one subject notifies interested listeners about a change.
Observer, Mediator, and event bus patterns can all reduce direct coupling, but they solve different communication problems. Observer fits one-to-many notification. Mediator fits centralized collaboration rules. Event bus fits broader publish-subscribe distribution across a system or subsystem.
Java systems often need notifications, orchestration, and distributed event flow. If these communication styles blur together, code becomes harder to understand and architectural boundaries become muddy.
Observer is usually the lightest-weight option: a subject notifies its observers. Mediator is stronger when the problem is not notification but coordination among participants. An event bus goes wider still, acting as an event distribution mechanism across a larger boundary.
In Java applications, these patterns often show up near each other. Domain events may look observer-like, application orchestration may look mediator-like, and asynchronous infrastructure may look event-bus-like. Naming the force precisely keeps the design clearer.
Choose Observer for listeners, Mediator for collaboration logic, and event bus for broader event distribution across components.
Observer is best when one subject notifies interested listeners about a change.
Mediator is best when interaction logic among participants needs one coordination point.
Event bus is best when events should flow across a broader set of publishers and subscribers with looser coupling.
package org.javaomnibus.ecommerce.gof.behavioral;
public final class CommunicationChoices {
OrderConfirmedPublisher observerPublisher() { return null; }
CheckoutMediator workflowMediator() { return null; }
DomainEventBus eventBus() { return null; }
}
| Pattern | Main force | Scope | Typical Java example |
|---|---|---|---|
| Observer | One-to-many notification | Local subject/listener relationship | Order confirmed listeners |
| Mediator | Centralized collaboration | Coordinating participants in one flow | Checkout orchestration |
| Event Bus | Broader event distribution | Subsystem or application-wide messaging | Domain event distribution |
Loose coupling can improve extensibility but can also hide control flow. The broader the communication model becomes, the more observability and naming discipline matter.