Exam 2 - L17 - Observer Design Pattern Flashcards
What is the main purpose of the Observer Design pattern?
To allow multiple objects (observers) to automatically respond to changes in another object (subject/publisher), promoting loose coupling.
What is loose coupling?
Classes have few dependencies
What are the key components in the Observer Pattern?
Subject (Publisher) - Maintains list of observers, provides attach/detach/notify methods.
Observer (Subscriber) - Interface that defines the update() method.
What does the subscription mechanism involve?
- A list to store subscribers
- Public methods to add/remove subscribers
- A ‘notifyObservers()’ method to call each observer’s ‘update()’ method
What are the advantages of using the Observer Pattern?
Loose coupling
Reusability and modularity
Broadcasting capability to multiple observers
What are some potential downsides of the Observer Pattern?
Memory leaks (if observers aren’t detached)
Implementation complexity