O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Minimalist Abstract Observer

To launch an Observer application using the minimum essentials, we need only two interfaces and two classes, reflecting the model in the class diagram (Figure 8-2):

  • A subject interface

  • An observer interface

  • A concrete subject

  • A concrete observer

Keeping the overall goal in mind to centrally distribute state information, the constructs boil down to a subscribing method, an unsubscribing method, a notification method for the subject, and an update method for the observer. The key to making this all work right is the use of an array to hold the subscribing observers, and a distribution method for broadcasting the current state.

Subject Interface

All we need for the Subject interface are three methods to take care of the subscription and notification work. Because all of the functions in an interface construct are abstract, we don't have a lot of detail to address. However, we've got to be careful to be sure that all the necessary parts are in place. Example 8-5 shows the script to be saved as Subject.as:

Example 8-5. Subject.as

package
{
    //Subject Interface
    public interface Subject
    {
        function subscribeObserver(o:Observer):void;
        function unsubscribeObserver(o:Observer):void;
        function notifyObserver():void;
    }
}

In the first two functions, you'll see evidence of composition. The "o" parameter is an Observer datatype, which is a reference to another interface that'll be built as part of the Observer design pattern. If you look at the diagram in Figure 8-2, you'll ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required