When to notify
The final thing to consider when using the Observer pattern is when to notify the Observers. The first option is to notify the Observers after any data has changed. In the above example, the Player called Notify within the AdjustHealth method. This means that the Observers will have the most up to date information immediately. However, if the AdjustHealth method is called many times in one frame, time is wasted updating the Observers many times. This problem becomes worse if the Observers were observing more than just the Player's health.
The other option would be to notify the Observers after all data has changed. This solution is obviously more efficient but a lot depends on how your system works. In the preceding example, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access