ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Minimalist Example of an MVC Pattern | 437

implementations in the MVC. Our view elements are going to get more comple xas


they can implement a third pattern, the composite (see Chapter 6 for examples of the


composite pattern). Implementing views as elements of a composite pattern only


makes sense for comple xnested user interfaces that contain multiple views. Nested


views bring several advantages to updating the user interface, as updates can cascade


down the composite view tree structure. Also, composite views can create and


remove child views based on application state and user mode. A good example of a


comple xuser interface is theProperties inspectorpanel in the Flash authoring envi-


ronment. TheProperties inspectoris context sensitive, and adds or removes user


interface elements based on the object selected on the stage.


Component and composite views


The first step is to create the component and composite classes for the view. These


classes should behave as abstract classes and should be subclassed and not instanti-


ated, as shown in Example 12-7.


Example 12-7. ComponentView.as


package
{
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.display.Sprite;


// ABSTRACT Class (should be subclassed and not instantiated)
public class ComponentView extends Sprite {
{
protected var model:Object;
protected var controller:Object;


public function ComponentView(aModel:Object, aController:Object = null)


{
this.model = aModel;
this.controller = aController;
}


public function add(c:ComponentView):void
{
throw new IllegalOperationError("add operation not supported");
}


public function remove(c:ComponentView):void
{
throw new IllegalOperationError("remove operation not supported");


}


public function getChild(n:int):ComponentView
{
throw new IllegalOperationError("getChild operation not supported");

Free download pdf