ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Hooking It Up | 353

new ActionScript files, and enter the code for Examples 9-14 to 9-17, saving them as


the caption name.


The hook function,checkWeather( ), returns a default value of “Nice and clear.” The


template method algorithm is made up of two operations,checkAirplane( )and con-


ditionally,fileVFR( ). Both are abstract functions whose details are left to the sub-


classes for specification. The condition for the fileVFR( )operation is a string


returned by thecheckWeather( )hook method. By setting the default to “Nice and


clear,” overriding thecheckWeather( ) method is usually unnecessary.


Example 9-14. BajaFlight.as


package
{
//Abstract Class
class BajaFlight
{
protected var weatherNow:String;


public final function templateMethod ( ):void
{
checkAirplane ( );
if (checkWeather( )=="Nice and clear")
{
fileVFR ( );
}
}
protected function fileVFR ( ):void
{
//Awaiting instructions
}
protected function checkAirplane ( ):void
{
//Awaiting instructions
}
//Hook function
protected function checkWeather ( ):String
{
weatherNow="Nice and clear";
return weatherNow;
}
}
}


Example 9-15. FlightPlan.as


package
{
//Any concrete class
public class FlightPlan extends BajaFlight
{
trace("^ Clear as a Bell ^");
override protected function checkAirplane( ):void

Free download pdf