Package ocempgui :: Package events :: Module Subject :: Class Subject
[show private | hide private]
[frames | no frames]

Type 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)
  add(self, *observers)
S.add (observers) -> None
  notify(self, prop, oldval, newval)
S.notify (...) -> None
  remove(self, observer)
S.remove (observer) -> None
  set_name(self, name)
S.set_name (...) -> None
    Inherited from object
  __delattr__(...)
x.__delattr__('name') <==> del x.name
  __getattribute__(...)
x.__getattribute__('name') <==> x.name
  __hash__(x)
x.__hash__() <==> hash(x)
  __new__(T, S, ...)
T.__new__(S, ...) -> a new object with type S, a subtype of T
  __reduce__(...)
helper for pickle
  __reduce_ex__(...)
helper for pickle
  __repr__(x)
x.__repr__() <==> repr(x)
  __setattr__(...)
x.__setattr__('name', value) <==> x.name = value
  __str__(x)
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

listeners

The observers for the Subject.
Get Method:
unknown-687693748(...)

name

The name of the Subject.
Get Method:
unknown-687693636(...)
Set Method:
unknown-687693692(...)

Generated by Epydoc 2.1 on Thu Jan 10 10:18:44 2008 http://epydoc.sf.net