Minimalist Example of an MVC Pattern | 441
Both component view classes CharCodeLeafView (Example 12-10) and
AsciiCharLeafView(Example 12-11) subclass theComponentViewclass (Example 12-8).
Note that they don’t receive any input from the user interface. These two views can
therefore be instantiated without passing a controller object to the constructor. The
controller will default tonull in this case.
Now all that’s left to do is build the MVC triad with the nested view. We’ll allow the
client to build the nested view and register the root node with the model to receive
update events.
Building the Nested View Structure
Building nested view structures is identical to developing composite structures using
the composite pattern. We have to visualize the view as a tree (an upside-down tree)
and use theadd( ) method in the composite view to add child views.
var model:IModel = new Model( );
var controller:IKeyboardInputHandler = new Controller(model);
override public function update(event:Event = null):void
{
// get data from model and update view
trace(model.getKey( ));
}
}
}
Example 12-11. AsciiCharLeafView.as
package
{
import flash.events.Event;
public class AsciiCharLeafView extends ComponentView
{
public function AsciiCharLeafView(aModel:IModel,aController:Object = null)
{
super(aModel, aController);
}
override public function update(event:Event = null):void
{
// get data from model and update view
trace(String.fromCharCode(model.getKey( )));
}
}
}
Example 12-10. CharCodeLeafView.as (continued)