2 from pprint
import pformat
4 class Observer(object):
5 '''Observers receive notify(*attributes) messages from their
6 subjects whenever new data arrives. This notify() message signifies
7 that an observer should update its internal state/view.'''
9 def __init__(self
, model
):
11 self
.model
.add_observer(self
)
15 self
.model
.remove_observer(self
)
17 def set_debug(self
, enabled
):
18 self
.__debug
= enabled
20 def notify(self
, *attributes
):
21 '''Called by the model to notify Observers about changes.'''
22 # We can be notified about multiple attribute changes at once
24 for attr
in attributes
:
25 notify
= model
.get_notify()
26 model
.set_notify(False) # NOTIFY OFF
28 value
= model
.get_param(attr
)
29 self
.subject_changed(attr
, value
)
31 model
.set_notify(notify
) # NOTIFY ON
33 def subject_changed(self
, attr
, value
):
34 '''This method handles updating of the observer/UI.
35 This must be implemented in each concrete observer class.'''
37 msg
= 'Concrete Observers must override subject_changed().'
38 raise NotImplementedError, msg