
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
354
|
Chapter 16: The Observer Design Pattern
The source code discussed in this chapter is available at http://moock.org/eas2/
examples.
Implementing Observer in ActionScript 2.0
The responsibilities of the subject class in the Observer pattern are as follows:
• Maintain a list of dependent observers
• Provide methods for adding (registering) and removing (unregistering) observer
objects
• Send notifications to observers
To create the subject class, we’ll follow Java’s implementation and define a base
class, Observable, that any would-be subject class can extend. The Observable class
provides the basic services required of all subject classes. Specifically, Observable
defines the following properties and methods:
observers
An instance property that holds an array of observers
addObserver( )
A method for adding a new observer to the
observers array
removeObserver( )
A method for removing an observer from the
observers array
notifyObservers( )
A method for sending change notifications to observers
Now let’s turn to the observer classes. The sole responsibility of all observer classes is
to provide a standard mechanism for receiving updates.
Certainly, an observer class can (and typically does) implement other behaviors
according to a particular situation. For example, our earlier TextReport class ...