ActionScript 3.0 Design Patterns

(Chris Devlin) #1

354 | Chapter 9: Template Method Pattern


Because both thecheckAirplane( )andfileVFR( )operations are abstract functions


(in intention, at least), they must be overridden. However, the hook method is being


treated as a concrete method, so we can optionally override it. In Example 9-15, the


hook methodcheckWeather( )is not included in the class. It’s inherited from the


BajaFlightclass and is employed without any changes or need to include it in the


class otherwise. However, in Example 9-16, we want to change the value of the


checkWeather( )hook method, so it is overridden, and “cloudy” is returned instead of


the default (“Nice and clear”). The template method only launches thefileVFR( )


methodifthe return value ofcheckWeather( )is “Nice and clear.” As a result, the


fileVFR( ) operation is never launched.


{

trace("Doing the walk around...looking good.");
}


override protected function fileVFR( ):void
{
trace("I'm off to Baja on a beautiful day!");
trace("First I'll file a VFR flightplan\n");
}
}
}


Example 9-16. Cloudy.as


package
{
//Any concrete class
public class Cloudy extends BajaFlight
{


trace ("^ Cloudy ^");
override protected function checkAirplane ( ):void
{
trace ("Doing the walk around...looking good");
}
override protected function fileVFR ( ):void
{
trace ("I'm off to Baja on a beautiful day!");
trace ("First I'll file a VFR flightplan\n");
}


//Invoking the hook
override protected function checkWeather ( ):String
{
weatherNow="Cloudy";
return weatherNow;
}
}
}


Example 9-15. FlightPlan.as (continued)

Free download pdf