Observer pattern problems
Fast recall
Quick revision
Core answer
Interview-ready guide
Interview answer
Observer trades direct coupling for indirect control flow. For example, one OrderUpdated callback may synchronously trigger inventory, email, and analytics handlers, so one slow or failing handler can delay the original operation. Recursive updates and ordering assumptions can also make the path hard to follow. If registration and notification happen concurrently, the observer list and subject state need a clear snapshot or locking design, and mutable payloads can let listeners interfere. The lifecycle problem occurs when a long-lived subject holds a strong reference to an observer that should have been removed; that mismatch prevents collection. I define timing, ordering, failure, retry, and cleanup policy rather than assuming the pattern provides them.
Observer pattern problems
Direct Answer
Observer can create hidden control flow, expensive or cascading notifications, implementation-dependent ordering, failure coupling, concurrency problems, and listener lifecycle leaks. A strong reference leaks only when the subject outlives a listener that should have been removed. Define delivery timing and error policy, keep payloads stable, manage registration, and use a suitable snapshot or synchronization strategy.
Common Mistakes & Pitfalls
- Never calling
removeObserver. Registering in a constructor and assuming GC will clean up — it won't, because the subject still references the observer. - Hidden ordering dependencies. Writing observer A to assume B already ran. Notification order isn't guaranteed, so this is a flaky bug.
- Slow synchronous observers. Doing a network/DB call inside
update()blocks the subject and every later observer. Offload to@Asyncor an executor. - Plain
ArrayListunder concurrency. Iterating during a concurrentaddthrowsConcurrentModificationException; useCopyOnWriteArrayList.
Check your understanding
Answer these without looking back at the article.
- How would you prevent memory leaks when using Observer in a long-running Java application?
- How does Java's WeakReference help with the lapsed listener problem?
- How would you make notification asynchronous without blocking the subject?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.