The Observer design pattern provides a solution:
Define
Subject
and
Observer
objects
so that when a subject changes state,
all registered observers are notified and updated
automatically.
Describing the Observer design in more detail is the
theme of the following sections.
See Applicability section for all problems Observer can solve.
Subject
and
Observer
objects:
Subject defines an interface
for registering and unregistering observers
(attach(o), detach(o))
and for notifying observers
(notify()), i.e.,
calling update()
on all registered observers.
Observer defines an interface
for updating state (update()), i.e.,
synchronizing observer's state
with subject's state.
for each o in observers: o.update()).