ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Extended Example: Infrared Weather Maps | 451

Note that only the combo bo xview that’s at the root of the composite view struc-


ture has registered with the model to receive update events. These will trickle down


from the combo box view to its child map view.


The views extend theSpriteclass. Therefore, the client has the flexibility to place


them on the stage that fits the required application layout.


Setting the Model to Self-Update


Changes in the model are not always initiated by user interaction. You may have


noticed a time stamp on the satellite images (in GMT). The time stamp tells us that


the satellite images are updated about every 15 minutes. We can easily add a self-


update timer to our model to dispatch an event every 15 minutes that tells the map


view to reload the updated image. We should add the following statements to the


end of theModelclass constructor in Example 12-13 (make sure to import the


flash.utils.Timer class first).


var updateTimer:Timer = new Timer(1000 * 60 * 15);
updateTimer.addEventListener("timer", timerHandler);
updateTimer.start( );

The following listener method should be added to theModelclass in Example 12-13.


The listener methodtimerHandler( )is set to listen for a newTimerEventto be dis-


patched every 15 minutes. ThetimerHandler( )calls theupdate( )method to dis-


patch an update event to registered observers.


public function timerHandler(event:TimerEvent):void {
this.update( );
}

Extended Example: Infrared Weather Maps


To illustrate how the MVC pattern allows for flexible expansion and reuse of its


model, view, and controller elements, we will extend our weather maps example.


You may have noticed that there are three types of satellite image maps on theGeo-


stationary Satellite Serverweb site (http://www.goes.noaa.gov). What if we want to


give the user the option of choosing whether to view a visible or infrared image?


What changes would be required to extend our weather maps application to view


infrared satellite images?


To begin with, we’ll need to add another view such as a radio button group to


choose the type of satellite image (visible or infrared as shown in Figure 12-5).


We will need to create a new controller to handle the user input to the new view ele-


ment. Our model will also need to hold more data, as we need to integrate five addi-


tional image URLs, one for each region as an infrared image. The application logic in


the model will also need to be updated to handle the new data. Can we add all these


new features without changing existing code? Can we leverage the flexibility and


reuse of the MVC pattern without breaking anything?

Free download pdf