Observer

The ObserverProcess, Observable, and Observer classes permit an author to register and notify objects for state-change related events.

 

Sometimes it is desirable to have an object’s dependents notified of its state-changes.

 

The Observer Process

In order to be notified of state changes an umbrella mechanism must be used to allow for the registration of a collection of objects; comparison of these objects’ current states with snapshots of previous states; and notification of objects whose states have changes so that they might in turn notify their dependents of the state change event. In addition, it is often useful to provide information regarding the specific state change for the given object.

 

The ObserverProcess class provides the mechanism for registering Observables, objects whose state we wish to observe.

 

Adding an Observable Object to the Process

The addObservable() method adds a subject (an Observable class object) to the Observer Process. It also takes a snapshot of the subject, providing a baseline for future comparison and notification.

 

Removing an Observable Object from the Process

Sometimes it’s desirable to remove an object from state-change observation. The removeObservable() method does this.

 

Notifying Observables of State Changes

ObserverProcess’ notify() method must be executed each time an author wants to notify an Observable class object of state changes. This method might be called as a daemon, or manually in code. When it executes it will iterate over every registered Observable and compare its current state with the last snapshot taken for the object. If state-changes are found then a state delta list is produced and passed to the Observable, as well as a reference to the snapshot object.

 

Observable Objects

In order to register a class with the ObserverProcess class an object must inherit from Observable class. Observable class provides a mechanism for adding, removing, and notifying Observers.

 

Adding Observers

At any point in the program code an Observable object can add an Observer using the addObserver() method. Once registered the Observer will be notified by the Observable any time its state changes (when ObserverProcess.notify() executes).

 

Removing Observers

Similarly, removeObserver() will remove an Observer from an Observable’s notification list.

 

Notifying Observers

If ObserverProcess finds a state-change for the Observable then it sends a message to the Observable’s notifyObserver() method. This method iterates over the Observable’s Observers, sending a message indicating that a state change for the object has occurred.

 

Observers

Observers are objects that are dependent upon Observables and need to be notified of any state-change the Observable might have undergone between notifications.

 

In order to receive notifications as an Observer, an object must inherit from Observer class. This class provides one method, updateStateChange(), which receives a reference to the subject (the Observable), the image (the last snapshot of the Observable), and the delta (the state change delta list produced by the object’s snapshot).

 

By default this method displays the state-change delta list for debugging purposes and should be overridden by the author.

 

 

This file is part of the TADS 3 Proteus Library Extension

Copyright © 2001-2004 Kevin Forchione. All rights reserved.