intermediate

Observer

Notify subscribers about changes without hard coupling the subject to every listener, while controlling lifecycle and ordering.

Observer notifies dependents when subject state changes without hard-wiring every listener. Event emitters, reactive stores, DOM events, and pub/sub buses are modern variants. Design must address subscription lifecycle, delivery order, error isolation, and memory leaks from forgotten listeners.

On interviews: compare push versus pull models. Discuss backpressure, debouncing, and when events should be domain facts versus technical signals.

Common pitfalls: synchronous observers blocking publishers; listeners never unsubscribed; implicit ordering assumptions across subscribers.

The trade-off is flexibility versus complexity—know when the simpler path is enough.

Checklist:

  • Define unsubscribe or weak-reference policy.
  • Isolate listener failures from publisher.
  • Document event payload contracts and versioning.
  • Prefer immutable event records for auditability.