O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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 structure. 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 operation that will be constant throughout the subclasses.

Bare Bones Template Method

Example 9-1 and Example 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.

Example 9-1. AbstractClass.as

package
{
    //Abstract Class
    class AbstractClass
    {
        public final function templateMethod():void
        {
            primitiveA();
            primitiveB();
            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");
        }
    }
}

Looking at the AbstractClass ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required