Check out the new USENIX Web site. next up previous
Next: Observable Mixins Up: Case Study: Automatic Observables Previous: Case Study: Automatic Observables

Observer/Observable

The Observer pattern is a well-known design pattern (also known as Model/View), discussed in [GHJV95]. In it, whenever an Observable object changes state, its Observer classes are notified. This pattern is used when there are common classes that encapsulate state, and other classes that represent a ``view'' of that state.

Sun's JDK provides an implementation of Observer/Observable in Java. In it, Observer is an interface containing the single method Update(Observable, Object), which is called by an Observable object's notifyObservers method whenever it wishes to inform its Observers that its state has changed. User classes acting as Observers need only implement the interface, call the addObserver method of an Observable instance, and they will be able to begin observation.

In contrast, Observable is implemented as a class which includes methods for managing Observer lists and notifying Observers, as well as code to check, set, and clear a hasChanged bit. Programmers define Observable classes by subclassing the Observable base class. Unfortunately, requiring authors to use a specific base class in this way is often limiting. For example, third-party software cannot be made Observable without changing an existing class hierarchy.



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