Simple definition#
The Observer pattern lets an object (the subject) keep a list of interested objects (observers) and notify all of them automatically when something happens. The subject knows only that observers implement a small interface; it does not know what they do.
GoF intent: “Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.” Also known as Dependents and Publish-Subscribe.
The problem#
When an order is placed, the shop must send a confirmation email, award loyalty points, record analytics and run fraud checks. If OrderService.placeOrder() calls each of these services directly:
- the order service depends on every downstream concern and grows with each new one
- adding a feature (a Slack notification) means editing order code
- one failing concern can break order placement.
Real-world analogy#
A newspaper subscription. The publisher does not know its readers personally; it delivers each edition to everyone on the subscription list. Readers subscribe and unsubscribe at will.
Structure#
- Subject (
EventPublisher<E>) — keeps observers, offers subscribe/unsubscribe, notifies. - Observer (
Consumer<E>) — the update interface. - Concrete observers (
ConfirmationEmailer,LoyaltyPoints, lambdas).
classDiagram
direction LR
class EventPublisher~E~ {
-List observers
+subscribe(Consumer) Subscription
+publish(E)
}
class Subscription {
<<interface>>
+close()
}
class Consumer~E~ {
<<interface>>
+accept(E)
}
class ConfirmationEmailer
class LoyaltyPoints
EventPublisher o--> "*" Consumer : notifies
EventPublisher ..> Subscription : returns
Consumer <|.. ConfirmationEmailer
Consumer <|.. LoyaltyPointsJava implementation#
The code is in examples/java/src/hub/patterns/observer/ObserverDemo.java.
/** Subject: keeps a list of observers and notifies them; knows nothing about what they do. */
final class EventPublisher<E> {
/** Handle returned to observers so they can unsubscribe (avoids lapsed-listener leaks). */
interface Subscription extends AutoCloseable {
@Override void close();
}
private final List<Consumer<? super E>> observers = new CopyOnWriteArrayList<>();
Subscription subscribe(Consumer<? super E> observer) {
observers.add(observer);
return () -> observers.remove(observer);
}
void publish(E event) {
for (Consumer<? super E> observer : observers) { // safe even if an observer unsubscribes now
try {
observer.accept(event);
} catch (RuntimeException e) { // one failing observer must not stop the rest
System.out.println(" observer failed: " + e.getMessage());
}
}
}
}final class ConfirmationEmailer implements Consumer<OrderPlaced> {
@Override public void accept(OrderPlaced e) {
System.out.println(" email: thanks " + e.customer() + " for order " + e.orderId());
}
}
final class LoyaltyPoints implements Consumer<OrderPlaced> {
@Override public void accept(OrderPlaced e) {
System.out.println(" loyalty: +" + e.total().intValue() + " points for " + e.customer());
}
}Publish ORD-1:
email: thanks ana for order ORD-1
loyalty: +129 points for ana
analytics: revenue 129.90
Publish ORD-2 (analytics unsubscribed):
email: thanks raj for order ORD-2
loyalty: +0 points for raj
observer failed: fraud check cannot score a zero-value orderStep-by-step walkthrough#
- Observers are just
Consumer<E>. Any lambda or class can subscribe; Java does not need a customObserverinterface (andjava.util.Observeris deprecated). subscribereturns aSubscription. Callers keep it andclose()it to unsubscribe. This prevents the lapsed listener leak, where forgotten observers keep objects alive forever.CopyOnWriteArrayListlets observers be added or removed while a notification is in progress, and makes concurrent subscription safe. It suits lists that are read far more often than they change.- Failures are isolated. The fraud check throws, but the email and loyalty observers still run. Decide this policy consciously: sometimes a failing observer should abort the operation.
- Notification is synchronous.
publishreturns after all observers ran, on the caller’s thread. A slow observer slows the publisher.
Push versus pull#
| Model | Subject sends | Pros | Cons |
|---|---|---|---|
| Push (this example) | An event with the relevant data | Observers need no reference back to the subject | The subject guesses what observers need |
| Pull | Just “I changed” (or itself) | Observers fetch exactly what they need | Observers depend on the subject’s API; extra calls |
Modern code mostly pushes immutable event objects (records), which can also be logged and sent across process boundaries.
From in-process observers to messaging#
| Scope | Mechanism | Delivery |
|---|---|---|
| One object graph | Observer (this article) | Synchronous, in memory |
| One Spring application | ApplicationEventPublisher and @EventListener, @TransactionalEventListener | Sync or async; can run after commit |
| Streams of values with back-pressure | Reactive Streams (java.util.concurrent.Flow, Project Reactor) | Asynchronous, demand-driven |
| Several services | A message broker (Kafka, RabbitMQ) | Durable, asynchronous, at-least-once |
Advantages and disadvantages#
| Advantages | Disadvantages |
|---|---|
| Subject and observers are loosely coupled (only an interface) | Notification order is usually unspecified |
| New observers need no change to the subject (OCP) | Hard to follow control flow: “who reacts to this?” |
| Broadcast to any number of observers | Memory leaks if observers do not unsubscribe |
| Natural fit for UIs and domain events | Cascades: observers triggering more updates can loop or storm |
When to use it#
- A change in one object requires changing others, and you do not know how many or which.
- Domain events: “order placed”, “payment failed” with several independent reactions.
- UI models notifying views (MVC).
When not to use it#
- There is exactly one dependent: call it directly.
- The reactions must be atomic with the change and must all succeed: use explicit calls in a transaction.
- Interactions are complex and two-way: consider a Mediator.
Common mistakes#
- Modifying the observer list while iterating over an
ArrayList(ConcurrentModificationException). - Slow observers blocking the publisher thread; move heavy work to an executor.
- Observers that mutate the subject during notification, causing re-entrant updates.
- Assuming delivery order between observers.
Real-world uses#
java.beans.PropertyChangeSupportandPropertyChangeListener.- Swing/AWT and JavaFX listeners; JavaFX observable properties.
- Spring
ApplicationEvents and@EventListener; Spring Boot lifecycle events. java.util.concurrent.Flow(Reactive Streams) and Project Reactor’sFlux.java.util.Observable/Observer(deprecated since Java 9: not serializable, not thread-safe, andObservableis a class, not an interface).
Interview questions and answers#
What is the Observer pattern? A behavioral pattern where a subject maintains a list of observers and notifies them of changes, so dependents update automatically without the subject knowing their concrete types.
Why was java.util.Observable deprecated?
It is a class (forcing inheritance), its notification model is limited, it is not thread-safe in a useful way, and its events carry no type information. Use PropertyChangeSupport, Flow or your own typed publisher.
What is the lapsed-listener problem? A memory leak caused by observers that are never unsubscribed, so the subject keeps them reachable.
Observer versus pub/sub with a broker? Same idea; a broker adds durability, asynchrony and process isolation, and decouples publisher and subscriber in time as well as code.
Observer versus Mediator? Observer broadcasts without coordinating; a mediator knows its participants and directs their interaction.
Summary#
- Observer is one-to-many notification through a small interface.
- Return unsubscribe handles, isolate failures, and keep notification fast.
- Spring events, reactive streams and message brokers are the same idea at larger scales.
- Durable side effects need an outbox and a broker, not in-memory observers.
Related topics#
- Application Events and Listeners
- Observer vs Mediator vs Event Bus
- Spring WebFlux and Project Reactor
- Event-Driven Microservices with Spring Cloud Stream
References#
- Gamma, Helm, Johnson, Vlissides. Design Patterns. Addison-Wesley, 1994. Observer, p. 293.
- Eric Freeman, Elisabeth Robson. Head First Design Patterns (2nd edition). O’Reilly, 2020. Chapter 2 (the weather station).
- Doug Lea. Concurrent Programming in Java: Design Principles and Patterns. Observer designs and completion callbacks.