ActionScript 3.0 Design Patterns

(Chris Devlin) #1

438 | Chapter 12: Model-View-Controller Pattern


TheComponentViewclass shown in Example 12-7 defines the abstract interface for


component views. This is similar to the classic component class introduced in


Chapter 6, with a few key differences. TheComponentViewclass keeps references to


the model and view, and includes a constructor. Not all views handle user input, and


a component view can be constructed by just passing a model instance. Therefore,


theaControllerparameter has a default value ofnullin the constructor. Also note


that theComponentViewclass extends theSpriteclass. This makes sense as most views


draw a user interface on the stage. We can use the properties and methods imple-


mented in the built-inSprite class to draw and add objects to the display list.


Theupdate( )method should behave as an abstract method. Leaf views that subclass


ComponentViewmust override and implement theupdate( )method to update their


user interface. This method is the listener function that intercepts update notifica-


tions from the model. For this reason, a parameter of typeEventis passed to it. This


parameter also has a default value ofnull,allowingupdate( )to be called without


passing an event parameter. This is useful when initially drawing the user interface in


its default state, and our subsequent examples illustrate it.


TheCompositeViewclass extendsComponentViewand overrides the methods that deal


with handing child views. In Example 12-8, we will implement only the add( )


method for simplicity.


return null;
}


// ABSTRACT Method (must be overridden in a subclass)
public function update(event:Event = null):void {}
}
}


Example 12-8. CompositeView.as


package {


import flash.events.Event;


// ABSTRACT Class (should be subclassed and not instantiated)
public class CompositeView extends ComponentView
{
private var aChildren:Array;


public function CompositeView(aModel:Object,aController:Object = null)
{
super(aModel, aController);
this.aChildren = new Array( );
}


override public function add(c:ComponentView):void
{


Example 12-7. ComponentView.as (continued)

Free download pdf