ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Minimalist Example: Abstract Template Method | 339

Looking at theAbstractClassyou can see that the main algorithm is encapsulated in


thetemplateMethod( )function. Thefinalstatement indicates that the method is


locked up and cannot be changed by the subclasses. However, all that really means is


that theordercannot be changed by the subclasses. Both theprimitiveA( )and


primitiveB( )functions, which make up thetemplateMethod( ),are abstract. They


are awaiting the details from the subclasses. So while thetemplateMethod( )is locked,


the operations that go into it don’t have to be, and can be changed by the subclasses.


At the same time, if you want any of the operations that make up the template


method to be locked, you can do that as well. The functionfromTemplate( )is locked


in the same way as the main template method. As part of thetemplateMethod( )func-


tion, thefromTemplate( )operation cannot be changed by anoverridestatement as


the two abstract function can. So you can have the best of both worlds. Those opera-


fromTemplate( );
}
protected function primitiveA( ):void
{
//Awaiting instructions
}
protected function primitiveB( ):void
{
//Awaiting instructions
}
private final function fromTemplate( ):void
{
trace("Hello everybody!");
}
};
}


Example 9-2. ConcreteClass.as


package
{
//Any concrete class
public class ConcreteClass extends AbstractClass
{
trace("Concrete class");
override protected function primitiveA( ):void
{
trace("Special A");
}
override protected function primitiveB( ):void
{
trace("Special B");
}
}
}


Example 9-1. AbstractClass.as (continued)

Free download pdf