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

Employing Flexibility in the Template Method

Now that you have a better idea of how the Template Method design pattern's structure works, let's see how it can be usefully employed. In this next example, you'll see an implementation that uses the same template method for two different concrete classes. Figure 9-4 shows the class diagram:

Template method with two concrete classes

Figure 9-4. Template method with two concrete classes

The main abstract class, ShedMaker, includes three operations in the templateMethod() function. Two are abstract and will be overridden by the subclasses where the details will be supplied. The third operation is a final function, not to be overridden, and will be used in all subclasses.

The example looks at what you may need in creating a shed in general, and what different types of sheds may need. The example develops an algorithm that does the following:

  • Designs the shed

  • Determines which supplies will be required

  • Stores tools in the completed shed

All sheds can use these steps, but depending on the kind of shed, the details will vary in certain aspects. In this case, we're supposing that one shed will be made of wood and the other of corrugated steel. Because designing with wood is different than designing for steel, the design operation (doDesign()) is abstract. Likewise, determining which supplies you need is different as well, and so the determineSupplies() function is also abstract. However, ...

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