ActionScript 3.0 Design Patterns

(Chris Devlin) #1

340 | Chapter 9: Template Method Pattern


tions in the main algorithm that you want to be detailed by the subclasses, you leave


abstract, and to those you want to remain constant, you apply thefinal statement.


In the subclass,ConcreteClass, both abstract methods from theAbstractClasshave


been overridden. The subclass gives each a different content. However, the third


operation that was locked up,fromTemplate( ), is nowhere to be found. That’s


because it cannot be changed, and because it was inherited from theAbstractClass.


There’s no need to add anything.


Testing Templates


To test the minimalist example, create a single instance of the concrete class, and


then add the template method to the instance. As you saw in the concrete class, the


two abstract operations were given details, but nothing else was done. Save


Example 9-3 in the same folder as the other two files in the minimalist example using


the caption as the filename.


To test the example, open a new Flash document file, and type inTestTemplatein the


Document class window in the Properties panel. When you test the application, you


should see the following in your Output panel:


Concrete class
Special A
Special B
Hello everybody!

The output shows that the special details added by the subclass in addition to the


invariant method are in the order locked into the template method algorithm. Keep-


ing in mind that the example is to expose the structure of the Template Method, you


can see how some of the details are deferred to the subclass (Special A and Special B),


how the algorithm’s order is preserved, and how any constant (locked) operations


are made available.


Example 9-3. TestTemplate.as


package
{
import flash.display.Sprite;
public class TestTemplate extends Sprite
{
public function TestTemplate( )
{
var testPlate:AbstractClass=new ConcreteClass( );
testPlate.templateMethod( );
}
}
}

Free download pdf