ActionScript 3.0 Design Patterns

(Chris Devlin) #1

446 | Chapter 12: Model-View-Controller Pattern


Note theupdate( )method in theModelclass shown in Example 12-13. It dispatches


aCHANGEevent to registered observers. Theupdate( )method is called whenever the


application state changes.


As a developer, you may be tempted to store the region list in the view for simplic-


ity. However, this greatly reduces flexibility and reuse. Adding another region, for


example, would require changes to both the view and the model. It’s always a good


practice to adhere to the delineated responsibilities of the three MVC elements.


Application data should be accessible only through the model.


The Controller


The combo bo xview is the only interface element in the application that users can


control. The interface for the corresponding controller is shown in Example 12-14.


The implementation is show in Example 12-15.


public function getRegion( ):uint
{
return this.chosenRegion;
}


public function setRegion(index:uint):void
{
this.chosenRegion = index;
this.update( );
}


public function getMapURL( ):String
{
return this.aImageURLs[chosenRegion];
}


protected function update( ):void
{
dispatchEvent(new Event(Event.CHANGE)); // dispatch event
}
}
}


Example 12-14. ICompInputHandler.as


package
{
public interface ICompInputHandler
{
function compChangeHandler(index:uint):void
}
}


Example 12-13. Model.as (Model for the weather map example) (continued)

Free download pdf