Quick revision
The short answer
In the observer pattern, a subject keeps a list of observers and notifies them when its state changes, without knowing their concrete types. Java examples include PropertyChangeListener in java.beans, Spring's ApplicationEvent with @EventListener, and the Flow API for reactive streams. java.util.Observable was deprecated in Java 9 because it is a class rather than an interface, its event model is too limited, and it defines no thread-safety or ordering guarantees; in any implementation, observers that are never removed leak memory, and a slow observer slows the subject when notification is synchronous.
What to remember
In this answer, we saw that in the observer pattern a subject notifies registered observers through a common interface without knowing their types. PropertyChangeListener, the Flow API and Spring's @EventListener are everyday examples. java.util.Observable was deprecated in Java 9 because it is a class we must extend, it passes untyped events, and it defines no ordering or thread-safety guarantees. Every implementation must also unregister observers to avoid leaks and keep synchronous observers fast so they do not slow or break the subject.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
In the observer pattern, a subject keeps a list of observers and notifies them when its state changes, without knowing their concrete types. Java examples include PropertyChangeListener in java.beans, Spring's ApplicationEvent with @EventListener, and the Flow API for reactive streams. java.util.Observable was deprecated in Java 9 because it is a class rather than an interface, its event model is too limited, and it defines no thread-safety or ordering guarantees; in any implementation, observers that are never removed leak memory, and a slow observer slows the subject when notification is synchronous.
Why this question is asked
Observer is behind every listener, callback and event we use, from button clicks to Spring application events and Kafka consumers in spirit. It is the pattern that lets one part of a program react to another without the two being wired together directly. Interviewers ask about it because the concept is simple but the practical traps, leaks, ordering and slow listeners, show up in real systems, and because the Observable deprecation is a nice test of whether we know why an API failed. In this answer, we will recap how the pattern works, write a minimal version, look at the Java and Spring examples, explain the Observable deprecation, and then cover the two traps every implementation must handle.
1A Quick Recap of Observer
The pattern has two roles. The subject is the object whose state others care about, such as an order or a stock price. The observers are the objects that want to know when it changes. The subject keeps a list of observers, offers methods to register and unregister them, and when something happens it calls each observer through a common interface.
The point is loose coupling. The subject only knows the interface, not the concrete classes. An order does not need to know that an email sender, an analytics tracker and a stock reservation module all care about it being placed; each registers itself, and new observers can be added without changing the subject.
5 messages in order. Play them, or step through them one at a time.
CopyOnWriteArrayList is used so that a listener can unsubscribe, or another thread can subscribe, while the loop is running, without a ConcurrentModificationException. It copies the array on every change, which is fine because listeners change rarely and notifications are frequent.
2Observer in Java and Spring
The JDK has several observers. In java.beans, PropertyChangeSupport lets a class fire PropertyChangeEvent objects carrying the property name, old value and new value to registered PropertyChangeListener instances. Swing and AWT listeners, such as ActionListener, are observers of UI components. The Flow API, added in Java 9 in java.util.concurrent.Flow, defines Publisher, Subscriber, Subscription and Processor, the interfaces of reactive streams, where the subscriber also controls how many items it wants, a feature called backpressure.
In Spring, the application context is a subject. Any bean can publish an event through ApplicationEventPublisher, and any bean method annotated with @EventListener receives events of its parameter type. The publisher does not know who listens.
Notice that since Spring 4.2 an event can be any object, such as a record; it no longer has to extend ApplicationEvent. By default these listeners run synchronously in the publisher's thread, which matters for the traps below.
3Why java.util.Observable Was Deprecated
Java 1.0 shipped java.util.Observable and the Observer interface, and Java 9 deprecated both. The first reason is that Observable is a class, not an interface. A subject had to extend it, and since Java has single inheritance, a class that already extended something else could not use it. Its key method setChanged() is protected, so we cannot even compose an Observable as a field without subclassing it.
The second reason is the event model. Observer.update(Observable o, Object arg) passes an untyped Object, so every observer casts and guesses what changed. There is no way to say which property changed or to have different event types. The third reason is that it defines no guarantees: the order in which observers are notified is unspecified, and the thread-safety of notification is not defined, so it was not suitable for concurrent code. The deprecation note points to java.beans for richer events and to java.util.concurrent, including Flow, for reliable, ordered messaging between threads.
| Aspect | java.util.Observable | Replacements (java.beans, Flow, Spring) |
|---|---|---|
| Type | A class we must extend | Interfaces, used by composition |
| Event data | Untyped Object argument | Typed events: property, old and new value, records |
| Ordering | Unspecified | Defined per API or controllable, for example @Order |
| Threads | No guarantees | Flow and concurrent tools define the rules |
4Trap 1: Observers That Are Never Removed
The subject holds strong references to its observers. If a short-lived object registers with a long-lived subject and never unregisters, the subject keeps it alive forever, together with everything it references. This is the lapsed listener problem, a classic source of memory leaks in UI code and in caches of listeners. A screen that registers on every open and never unregisters will leave one copy per open in memory.
The fix is to make unregistering part of the observer's lifecycle: remove it in a close() method, a try-finally, or when the component is destroyed. Spring manages this for @EventListener beans, because the context owns both sides. Weak references are sometimes used, but they make the behaviour depend on garbage collection timing, so explicit removal is clearer.
5Trap 2: Slow or Failing Observers
With synchronous notification, the subject calls observers one after another in its own thread. If the email observer takes two seconds, the subject waits two seconds, and so does every observer after it. If an observer throws, the exception usually propagates to the subject, and in many implementations the remaining observers are not notified at all. In Spring, an exception in a synchronous @EventListener propagates to the code that called publishEvent.
So we keep synchronous observers short and safe, and move slow work to another thread, for example with @Async on a Spring listener or by handing work to an executor. That introduces new questions about ordering and lost events if the process stops, which is the trade-off every event system has to make.
6Follow-up questions
What is the difference between observer and publish-subscribe?
In the classic observer, observers register directly with the subject, and the subject calls them. In publish-subscribe, a broker or event bus sits between them, so publishers and subscribers do not know each other at all and may run in different processes. Spring's application events are closer to publish-subscribe inside one process, and Kafka is publish-subscribe across processes.
Does Spring call @EventListener methods in a defined order?
By default the order is not something to rely on, but we can add @Order to listener methods or implement Ordered to control it. If listeners depend on each other's order, that often signals they should be one listener or a direct call.
How does the Flow API improve on plain observers?
A Flow.Subscriber receives a Subscription and calls request(n) to say how many items it can handle, so a fast publisher cannot overwhelm a slow subscriber. It also has explicit onError and onComplete signals, which plain observer interfaces lack.
7Conclusion
In this answer, we saw that in the observer pattern a subject notifies registered observers through a common interface without knowing their types. PropertyChangeListener, the Flow API and Spring's @EventListener are everyday examples. java.util.Observable was deprecated in Java 9 because it is a class we must extend, it passes untyped events, and it defines no ordering or thread-safety guarantees. Every implementation must also unregister observers to avoid leaks and keep synchronous observers fast so they do not slow or break the subject.
Open with
The observer pattern is about one object, the subject, notifying others when its state changes, without knowing who they are. The subject keeps a list of observers behind a common interface, offers subscribe and unsubscribe, and calls each observer when something happens.
Then, if they want more
That keeps the subject decoupled: an order does not need to know about the email sender or the analytics tracker. In Java we see it in PropertyChangeSupport and PropertyChangeListener in java.beans, in Swing listeners, and in the Flow API from Java 9, which adds backpressure for reactive streams.
In Spring, any bean can publish an event, which can be a plain record, and methods annotated with EventListener receive it. java.util.Observable was deprecated in Java 9 for three reasons. It is a class, so you had to extend it, which wastes your single inheritance, and setChanged is protected so you cannot compose it.
Its update method passes an untyped Object, so the event model is very limited. And it guarantees nothing about notification order or thread safety. Two traps apply to every implementation. Observers that are never removed keep themselves alive through the subject's list, which leaks memory, so unsubscribing has to be part of the lifecycle.
And with synchronous notification, a slow or failing observer delays or breaks the subject and the observers after it, so I keep synchronous listeners quick and move slow work to another thread, for example with Async in Spring.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- Java 9
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the official documentation for the version you use. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Explain the observer pattern, give Java and Spring examples, and say why...": https://scrivio-interview-library.netlify.app/java/behavioral-patterns/b189982a00/