338 | Chapter 9: Template Method Pattern
Minimalist Example: Abstract Template Method
Even though it may not make a lot of sense to use a Template Method design pattern
when you have only a single subclass, doing so helps reveal the design pattern’s struc-
ture. So, in this minimalist example, you should be able to easily see the underlying
structure used in the Template Method. We don’t need much, just two main classes:
- Abstract class with the algorithm for the template method
- At least one concrete class as a subclass of the abstract class
Look for the operations that will be detailed by the subclasses, and the one opera-
tion that will be constant throughout the subclasses.
Bare Bones Template Method
Examples 9-1 and 9-2 show the two classes we need to get started with this simple
but useful pattern. Save each class using the caption name as the filename.
Figure 9-3. Hollywood principle
Example 9-1. AbstractClass.as
package
{
//Abstract Class
class AbstractClass
{
public final function templateMethod( ):void
{
primitiveA( );
primitiveB( );
Abstract Class
operationA()
operationB()
template method()
operationA()
operationB()
(locked algorithm)
Responsible for order of
operations in algorithm
Tucked inside of
locked algorithm
Concrete Class
operationA()
operationB()
Concrete Class
operationA()
operationB()
Specification of operations, but not algorithm.
Calls from template method, but not subclass