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 Example of an MVC Pattern

This simple example keeps track of the last key pressed. When a new key is pressed, it changes the model and informs the view to update itself. The view uses the Flash output panel to print the character code of the key that's pressed. The character code is the numeric value of that key in the current character set. This example is meant to clarify how the observer, strategy, and composite patterns are integrated within the MVC.

Model as a Concrete Subject in an Observer Pattern

The relationship between the model and view is that of subject and observer (see Chapter 8). The model has to implement the subject interface that's part of the observer pattern. Fortuitously, ActionScript 3.0 has built in classes that do this already, using the ActionScript event model to notify observers of changes.

The EventDispatcher class in ActionScript 3.0

The EventDispatcher class implements the IEventDispatcher interface. Among other methods, the IEventDispatcher interface defines the following methods required of the subject in an observer pattern. (See AS3 documentation for a detailed explanation of all method parameters.)

addEventListener(type:String,
    listener:Function,
    useCapture:Boolean = false,
    priority:int = 0,
    useWeakReference:Boolean = false):void

removeEventListener(type:String,
    listener:Function,
    useCapture:Boolean = false):void

dispatchEvent(event:Event):Boolean

For the model to serve as a concrete subject in an observer pattern, it needs to implement the ...

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