Software engineeringLesson 3 of 4 in the course Design patterns

Observer Pattern: Notifications, Publish–Subscribe, and Lifecycle

5 flashcards · 5 quiz questions · 5 match pairs · 5 fill-in sentences · English

Lesson progress
0 / 5

Your progress is kept in this browser only.Sign up to save it

Summary

The Observer pattern provides a standard way for one object to notify other objects when its state changes. It is useful when several parts of a program need to react to the same event without making the event source depend on each listener’s specific implementation.

The Observer pattern is a behavioral design pattern that defines a one-to-many dependency between objects. One object, the subject, maintains a list of dependent objects, called observers. When the subject’s state changes, it notifies its registered observers.

All flashcards

Observer pattern
A behavioral pattern in which a subject notifies registered observers when its state changes.
Observer interface
A shared notification contract that lets a subject notify observers without depending on their concrete classes.
Publish–subscribe
A communication approach that commonly uses a broker or event bus between publishers and subscribers.
Lapsed listener problem
An unneeded observer remains reachable because a subject retains a reference to it.
Throttling
Limiting how often observers are notified when state changes occur rapidly.

Quiz questions

  1. How does a typical publish–subscribe system differ from a basic Observer implementation?Answer: A broker or event bus mediates between publishers and subscribers.
  2. In a typical basic Observer implementation, how are notifications commonly delivered?Answer: The subject invokes observers synchronously during its notification process.
  3. What can cause the lapsed listener problem?Answer: A subject retains a reference to an observer that is no longer needed.
  4. Why might an application throttle notifications to observers?Answer: To limit update frequency when rapid changes would make processing wasteful.
  5. How can weak references help with observer lifecycle management?Answer: They can avoid keeping an observer alive solely through the subject’s reference.

Based on the Wikipedia article Observer pattern, licensed CC BY-SA 4.0. Adapted into a summary, flashcards and quizzes.

All Software engineering lessons