Template Method


A Template Method is defined in an abstract class that sets a general algorithm made up (at least partially) of abstract methods. The steps of that algorithm are defined when subclasses override the abstract methods. The structure of the algorithm is maintained in the Template Method.

Consider the following example in which we have an abstract class that defines the way games are initialized:

package com.peachpit.aas3wdp.factoryexample {    public class AbstractGame {       // Template Method       public final function initialize():void {          createField();          createTeam("red");          createTeam("blue");          startGame();       }       public function createField():void {          throw new Error("Abstract Method!");      }       public function createTeam(name:String):void {          throw new Error("Abstract Method!");       }       public function startGame():void {          throw new Error("Abstract Method!");       }    } }


The initialize() method in the preceding example is the Template Method. It defines how the game is initialized by first calling the createField() method, then creating the teams with the createTeam() method calls, and finally calling the startGame() method. However, the methods it calls are not functional in this class. It's the responsibility of the subclass to define exactly how the field and teams are created and how the game is started.

Now we will create a FootballGame class that extends our AbstractGame class. This subclass overrides the abstract methods that are called from the initialize() Template Method in the abstract base class.

package com.peachpit.aas3wdp.factoryexample {    public class FootballGame extends AbstractGame {       public override function createField():void {          trace("Create Football Field");       }       public override function createTeam(name:String):void {          trace("Create Football Team Named " + name);       }       public override function startGame():void {          trace("Start Football Game");       }    } }


As you can see, our FootballGame class overrides the createField(), createTeam(), and startGame() methods to make them specific to football. However, the initialization algorithm is maintained. You can see how this same technique could also be used to build a BaseballGame or a BastketballGame class. We can run the example using the following client code:

package com.peachpit.aas3wdp.factoryexample {    import com.peachpit.aas3wdp.factoryexample.FootballGame;    import flash.display.Sprite;    public class FactoryExample extends Sprite {       public function FactoryExample() {          // Create an instance of FootballGame          var game:FootballGame = new FootballGame();          // Call the template method defined in AbstractGame          game.initialize();       }    } }


The following shows the output from the preceding example. As you can see, the overridden methods in the subclass were called by the Template Method. The algorithm was maintained in the Template Method while the details were deferred to subclass methods.

Create Football Field Create Football Team Named red Create Football Team Named blue Start Football Game





Advanced ActionScript 3 with Design Patterns
Advanced ActionScript 3 with Design Patterns
ISBN: 0321426568
EAN: 2147483647
Year: 2004
Pages: 132

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net