1 # Copyright (c) 2008 David Aguilar
2 """This module provides the Observer design pattern.
5 class Observer(object):
6 """Implements the Observer pattern for a single subject"""
8 def __init__(self
, model
):
10 self
.model
.add_observer(self
)
13 self
.model
.remove_observer(self
)
15 def notify(self
, *attributes
):
16 """Called by the model to notify Observers about changes."""
17 # We can be notified about multiple attribute changes at once
19 for attr
in attributes
:
20 notify
= model
.notification_enabled
21 model
.notification_enabled
= False # NOTIFY OFF
23 value
= model
.param(attr
)
24 self
.subject_changed(attr
, value
)
26 model
.notification_enabled
= notify
# NOTIFY ON
28 def subject_changed(self
, attr
, value
):
30 Updates the observer/view
32 This must be implemented by concrete observers class.
35 msg
= 'Concrete Observers must override subject_changed().'
36 raise NotImplementedError(msg
)