ActionScript 3.0 Design Patterns

(Chris Devlin) #1

440 | Chapter 12: Model-View-Controller Pattern


update notices from the model. TheRootNodeViewcomposite view will have two child


component views called CharCodeLeafView and AsciiCharLeafView. The


CharCodeLeafViewwill trace thecharacter codefor the last key pressed. Similarly, the


AsciiCharLeafViewwill trace theASCII charactercorresponding to the character


code. Let’s create the composite view first, as shown in Example 12-9.


TheRootNodeViewclass shown in Example 12-9 subclasses theCompositeViewclass


(Example 12-8). It does not draw a user interface; it simply listens for key press


events and delegates to the controller to handle them. Note thesuperstatement in


the constructor. This is required to call the constructor in the superclass. We can


now create the two component views (Examples 12-10 and 12-11).


Example 12-9. RootNodeView.as


package
{
import flash.events.;
import flash.display.
;


public class RootNodeView extends CompositeView
{
public function RootNodeView (aModel:IModel,


{
super(aModel, aController);


// register to receive key press notifications form the stage
target.addEventListener(KeyboardEvent.KEY_DOWN,


}


private function onKeyPress(event:KeyboardEvent):void
{
// delegate to the controller (strategy) to handle it
(controller as IKeyboardInputHandler).keyPressHandler(event);
}
}
}


Example 12-10. CharCodeLeafView.as


package
{
import flash.events.Event;


public class CharCodeLeafView extends ComponentView
{
public function CharCodeLeafView(aModel:IModel, aController:Object = null)
{
super(aModel, aController);
}

Free download pdf