
This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
Implementing Observer in ActionScript 2.0
|
359
Example 16-2 shows the source code for the Observer interface, which resides in the
util package along with Observable. The Observer interface is simple, containing only
one method: update( ). We use Observer to guarantee that any class that registers for
updates from a subject implements the standard update( ) method. Notice that the
update( ) method defines two parameters,
o and infoObj. The parameter o contains a
reference to the subject that changed. It is used to distinguish changes in one subject
from changes in another, as well as to access changed data in the subject (if the sub-
ject is using the pull model to update its observers). The parameter
infoObj receives
the optional info object provided by the subject if the subject is using the push model
to broadcast changes to its observers. Note that although a single observer can, in
theory, register with multiple subjects (and our code accommodates such a circum-
stance), in practice, a single observer typically registers with only one subject. An
observer registered with multiple subjects must use awkward conditionals (if/else or
switch statements) to distinguish between those subjects at update time. When a pro-
gram reaches that level of complexity, it’s often better to implement ...