Up to this point, all the Observer patterns' testing has been done by static assignments of values to the state setters. The purpose was to see how the structure of the pattern works, but for most practical applications using Flash and ActionScript 3.0, we should look at something where the state values are rapidly changing, and new updates have to be sent almost constantly.
To see how rapidly changing data can be sent to a single source, and then dispatched to multiple observers, this next application depicts an action game with three combatants. Two are made up of spaceships with a missile or torpedo and a space station that shoots a beam at the spaceships. There's really nothing much you can do with the game other than fire the two spaceships and space station, and watch the two spaceships get destroyed, but it serves to illustrate how all of the hits are generated by the action on the stage and broadcast to the different combatants. When a combatant is knocked out of play, he's no longer subscribed and all that is visible is the last message before destruction.
In setting up this particular Observer application, the
Observer interfaces contain only Number and String parameters in
the observer notification and update functions. The subscribe and unsubscribe
functions are the same as previous Observer designs in this chapter. Save the
classes in Example 8-15 and Example 8-16
by their caption names.
Example 8-15. Subject.as ...