Observer Pattern

Noun · Development

Definitions

  1. The Observer pattern is a behavioral design pattern where an object (the subject) maintains a list of dependents (observers) and notifies them automatically when its state changes. This decouples the event source from its handlers: the subject does not need to know what observers do with the notification. Observers register and unregister dynamically, making the pattern flexible. It is foundational to event-driven programming, GUI frameworks (click and input handlers), reactive libraries (RxJS, Reactor), and pub/sub systems. JavaScript's addEventListener() is a direct application. While powerful for loose coupling, the pattern can cause memory leaks if observers are not unregistered and can make control flow harder to trace.

    In plain English: A pattern where objects can subscribe to be notified when something changes, without the source knowing who's listening.

    Example: "The user model uses the observer pattern — when a user is created, it notifies the email service, analytics, and cache without knowing about them."

Related Terms