Check out the new USENIX Web site. next up previous
Next: The Key to Pandora's Up: Case Study: Automatic Observables Previous: Observer/Observable

Observable Mixins

The problems with the Observable base class could be avoided through the use of mixins. An Observable mixin would mix the Observable code, including the hasChanged bit, into an existing class, without modifying the inheritance hierarchy. However, with this approach, the class author is responsible for properly instrumenting the code to manage the hasChanged bit and call notifyObservers at appropriate times. Unfortunately, the traditional mixin model is not capable of changing existing methods to include new method calls.

However, Observable is easily implemented as an active mixin using a JOIE transformer. Figure 1 shows a fragment of the code for the JOIE transformer for Observable. The implementation resides in the single method transform called by the JOIE ClassLoader (of course, transformers may have additional methods). The transformer first adds a new boolean field, ``dirty'', to the class. It then scans each instruction of each non-static method defined in that class. If the instruction changes an instance field (e.g., the putfield instruction), the transformer inserts a code sequence that sets the dirty bit. The counter skips over those new instructions, and the scan continues. Not shown in the example is code that inserts the method call to notify the observers, or to mix in the methods to return the dirty bit, notify observers, or manipulate the list of observers.

This transformer is meant to be illustrative only. A more efficient scheme would build and traverse the control flow graphs, setting the dirty bit at most once per path, and adding the notify call only to exit blocks that could follow a block containing a set to dirty. A more universal solution would also perform an interprocedural search on member objects, and also handle inheritance (only a superclass needs to have the methods and the dirty bit mixed in, but all subclass methods must be scanned and spliced).


next up previous
Next: The Key to Pandora's Up: Case Study: Automatic Observables Previous: Observer/Observable

Geoff Alex Cohen
Tue Apr 28 14:31:49 EDT 1998