ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Hooking It Up | 355

In Example 9-16, thefileVFR( )function shows code that has been overridden, and


it contains the exact same content as in theFlightPlanclass. This was done inten-


tionally to illustrate that it doesn’t matter what code you place in thefileVFR( )


method in this class. Because the hook method has changed the return string that


prevents thefileVFR( ) function from executing, its content is immaterial.


Unlike Java and other languages where you can create actual abstract
functions, in ActionScript 3.0 (following ECMAScript standards) we
have topretendthat certain methods are abstract. As part of that fic-
tion, the fileVFR( )method is overridden in Example 9-16. The
method,fileVFR( ), could have been completely left out of theCloudy
class. It is not going to be launched because the hook method pre-
vents it. By changing the hook return value to something other than
“Nice and clear,” thefileVFR( )method won’t launch. So the only rea-
son that thefileVFR( )method is included in the class at all is to help
understand it as an abstract method that must be overridden by every
class that subclasses the main abstract class,BajaFlight.
By learning design patterns from examples where abstract functions
do exist, you’ll find it easier if you have “abstract” functions of your
own in your ActionScript 3.0 applications. Also, in certain design pat-
terns such as the Template Method, the hook methods are differenti-
ated from the abstract methods because all abstract methods must be
overridden, while hook methods need to be overridden only when
they’re changed by a subclass.

Finally, we’re all set to test the application. Ideally, we would have received data


from a weather service that we could use to indicate whether or not the conditions


were favorable for VFR flight. Instead, theTestFlightclass in Example 9-17 simply


creates instances of both theFlightPlan andCloudy classes.


Example 9-17. TestFlight.as


package
{
import flash.display.Sprite;
public class TestFlight extends Sprite
{
public function TestFlight( )
{
var takeOff:BajaFlight=new FlightPlan( );
takeOff.templateMethod( );


var grounded:BajaFlight=new Cloudy( );
grounded.templateMethod( );
}
}
}

Free download pdf