Observer
Implementation
Several issues related to the implementation of the dependency mechanism are discussed in this section.
-
-
-
Who triggers the update?
The subject and its observers rely on the notification mechanism to
stay consistent. But what object actually calls Notify to trigger the
update? Here are two options:
- Have state-setting operations on Subject call Notify after they
change the subject's state. The advantage of this approach is that
clients don't have to remember to call Notify on the subject. The
disadvantage is that several consecutive operations will cause
several consecutive updates, which may be inefficient.
- Make clients responsible for calling Notify at the right time.
The advantage here is that the client can wait to trigger the update
until after a series of state changes has been made, thereby
avoiding needless intermediate updates. The disadvantage is that
clients have an added responsibility to trigger the update. That
makes errors more likely, since clients might forget to call Notify.