In the Observer pattern, which component is responsible for notifying all registered observers about a change?
Think about who holds the list of observers and triggers updates.
The Subject maintains a list of observers and notifies them when its state changes.
Which data structure is best suited for storing observers in the Subject to efficiently add, remove, and notify them?
Consider the need to avoid duplicate observers and fast removal.
A HashSet prevents duplicates and allows fast add/remove operations, making it ideal for observer storage.
When the number of observers grows very large, what is a common technique to improve notification performance?
Think about how to avoid blocking the Subject during notifications.
Batching notifications and sending them asynchronously with a thread pool improves scalability and responsiveness.
What is a key tradeoff when choosing between the push and pull models for notifying observers?
Consider who controls the data flow and how much data is sent.
Push sends data proactively, which can increase bandwidth usage. Pull requires observers to request data, adding complexity.
Given the following steps in the Observer pattern, what is the correct order of operations when the Subject's state changes?
Think about what must happen before notifications and what observers do after receiving them.
The Subject first updates its state, then notifies observers, who then receive the notification and update themselves.
