| Home | Trees | Index | Help |
|---|
| Package ocempgui :: Package events :: Module Subject :: Class Subject |
|
object --+
|
Subject
Subject (name) -> Subject
A class matching the subscriptable Subject of the observer pattern.
The Subject class allows other classes to be notified whenever its
state(s) change. It has an unique name which identifies itself upon
notification of the registered classes.
The unique name of the concrete subject implementation can be
adjusted using the 'name' attribute and set_name() method.
subject.name = 'MySubject'
subject.set_name ('AnotherSubject')
Observers will be notified about state changes through an update()
method, they have to implement. The IObserver interface class
contains details about its signature.
To notify observers about a state change, the Subject implementation
must invoke its notify() method with the state, that changed, its
old state value and the newly set value.
class MySubject (Subject):
def __init__ (self):
Subject.__init__ (self, 'UniqueSubjectName')
self._value = None
def set_value (self, value):
# Preserve old value.
oldval = self._value
# Set new value.
self._value = value
# Notify observers
self.notify ('value', old, value)
A listening observer which should react upon changes of that value
could implement the reaction the following way:
class MyObserver (IObserver):
...
def update (self, subject, attribute, oldval, newval):
# Look for the certain subject.
if subject == 'UniqueSubjectName':
# Which state changed?
if attribute == 'value':
self.do_some_action (oldval, newval)
...
Attributes:
name - The name of the Subject.
listeners - List containing the attached observers.
| Method Summary | |
|---|---|
__init__(self,
name)
| |
S.add (observers) -> None | |
S.notify (...) -> None | |
S.remove (observer) -> None | |
S.set_name (...) -> None | |
| Inherited from object | |
x.__delattr__('name') <==> del x.name | |
x.__getattribute__('name') <==> x.name | |
x.__hash__() <==> hash(x) | |
T.__new__(S, ...) -> a new object with type S, a subtype of T | |
helper for pickle | |
helper for pickle | |
x.__repr__() <==> repr(x) | |
x.__setattr__('name', value) <==> x.name = value | |
x.__str__() <==> str(x) | |
| Property Summary | |
|---|---|
listeners: The observers for the Subject. | |
name: The name of the Subject. | |
| Method Details |
|---|
add(self, *observers)S.add (observers) -> None Adds one or more observers to the Subject. Raises an AttributeError, if one of the passed 'observers' arguments does not have a callable update() attribute. Raises a ValueError, if one of the passed 'observers' arguments is already registered as listener. |
notify(self, prop=None, oldval=None, newval=None)S.notify (...) -> None Notifies all observers about a state change. |
remove(self, observer)S.remove (observer) -> None Removes an observer from the Subject. |
set_name(self, name)S.set_name (...) -> None Sets the name of the Subject. Raises a TypeError, if the passed argument is not a string or unicode. |
| Property Details |
|---|
listenersThe observers for the Subject.
|
nameThe name of the Subject.
|
| Home | Trees | Index | Help |
|---|
| Generated by Epydoc 2.1 on Thu Jan 10 10:18:44 2008 | http://epydoc.sf.net |