ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Employing Flexibility in the Template Method | 343

In both concrete classes, atrace( )statement indicates which class is instantiated so


that you can see it in the output. More importantly, each of the two concrete classes


adds unique details to the two abstract operations, doDesign( ) and


determineSupplies( ). Neither includes anything regarding thestoreTools( )func-


tion. BecausestoreTools( )is invariant in all implementations, there’s no reason to


include it. It’s inherited from theShedMaker class.


To test it, both concrete classes are instantiated in the same testing class,BuildShed.


Example 9-7 shows that the two different instances use the interface data type to


instantiate thewoodandsteelinstances. Save the file using the example caption


name in the same directory as Examples 9-4 to 9-6.


Example 9-6. SteelShed.as


package
{
class SteelShed extends ShedMaker
{
trace("SteelShed");
override protected function doDesign( ):void
{
trace("Designing Steel Shed");
}
override protected function determineSupplies( ):void
{
trace("Ok I'll need some corregated sheet metal.")
trace("Better get some steel fasteners too.");
}
}
}


Example 9-7. BuildShed.as


package
{
import flash.display.Sprite;
public class BuildShed extends Sprite
{
public function BuildShed( )
{
//Make a steel shed
var steel:ShedMaker=new SteelShed( );
steel.templateMethod( );


//Make a wood shed
var wood:ShedMaker=new WoodShed( );
wood.templateMethod( );
}
}
}

Free download pdf