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.
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.
EventDispatcher class implements
IEventDispatcher interface. Among
other methods, the
interface defines the following methods required of the subject in an
observer pattern. (See AS3 documentation for a detailed explanation of all
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 ...