ActionScript 3.0 Design Patterns

(Chris Devlin) #1

454 | Chapter 12: Model-View-Controller Pattern


ThegetMapURL( )method was overridden, as the returned image URL now depends on


the currently chosen map type. Note that we didn’t modify any existing code, but


extended the existing model classModel to implement the new interface requirements.


Adding a New Controller


A new controller (Example 12-20) is necessary to handle the input to the map type


selector view. We can implement the same ICompInputHandler interface


(Example 12-14) for the new controller.


Adding a New View


We will add a new view (Example 12-21) that consists of two grouped radio buttons


that allow the user to select from either visible or infrared map images. The view will


use the built-inRadioButttoncomponent in Flash CS3. Make sure the radio button


component is dragged from the Components panel into the Library panel in the


Flash document.


override public function getMapURL( ):String {
switch(chosenMapType) {
case 1:
return this.aIRImageURLs[chosenRegion];
break;
default:
return this.aImageURLs[chosenRegion];
break;
}
}
}
}


Example 12-20. MapTypeController.as


package
{
public class MapTypeController implements ICompInputHandler {


private var model:Object;


public function MapTypeController(oModel:INewModel)
{
this.model = oModel;
}


public function compChangeHandler(index:uint):void
{
(model as INewModel).setMapType(index); // update model
}
}
}


Example 12-19. NewModel.as (continued)

Free download pdf