Template method

The strategy pattern allows for replacing an entire algorithm with a complimentary one. Frequently, replacing the entire algorithm is overkill: the vast majority of the algorithm remains the same in every strategy with only minor variations in specific sections.

The template method pattern is an approach which allows for some sections of an algorithm to be shared and other sections implemented using different approaches. These farmed out sections can be implemented by any one of a family of methods:

Template method

The template class implements parts of the algorithm and leaves other parts as abstract to later be overridden by classes which extend it. The inheritance hierarchy can be several layers deep, with each level implementing more and more of the template class.

Tip

An abstract class is one that contains abstract methods. Abstract methods are simply methods that have no body to them. The abstract class cannot be used directly and must, instead, be extended by another class that implements the abstract methods. An abstract class may extend another abstract class so that not all methods need to be implemented by the extending class.

This approach applies the principles of progressive enhancement to an algorithm. We move closer and closer to a fully implemented algorithm and, at the same time, build up an interesting inheritance tree. The template method helps keep identical code to a single location while still allowing for some deviation. A chain of partial implementations can be seen in the following diagram:

Template method

Overriding methods left as abstract is a quintessential part of object oriented programming. It is likely that this pattern is one which you've used frequently without even being aware that it had a name.

Implementation

I have been told, by those in the know, that there are many different ways to produce beer. These beers differ in their choice of ingredients and in their method of production. In fact beer does not even need to contain hops – it can be made from any number of grains. However there are similarities between all beers. They are all created through the fermentation process and all proper beers contain some alcohol content.

In Westeros there are a great number of craftsmen who pride themselves on creating top notch beers. We would like to describe their processes as a set of classes, each one describing a different beer making methodology. We start with a simplified implementation of creating a beer:

class BasicBeer {
  Create() {
    this.AddIngredients();
    this.Stir();
    this.Ferment();
    this.Test();
    if (this.TestingPassed()) {
      this.Distribute();
    }
  }
  AddIngredients() {
    throw "Add ingredients needs to be implemented";
  }
  Stir() {
    //stir 15 times with a wooden spoon
  }
  Ferment() {
    //let stand for 30 days
  }
  Test() {
    //draw off a cup of beer and taste it
  }
  TestingPassed() {
    throw "Conditions to pass a test must be implemented";
  }
  Distribute() {
    //place beer in 50L casks
  }
}

As there is no concept of abstract in JavaScript we've added exceptions to the various methods which must be overridden. The remaining methods can be changed but do not require it. An implementation of this for a raspberry beer would look like the following:

class RaspberryBeer extends BasicBeer {
  AddIngredients() {
    //add ingredients, probably including raspberries
  }
  TestingPassed() {
    //beer must be reddish and taste of raspberries
  }
}

Additional sub-classing may be performed at this stage for more specific raspberry beers.

The template method remains a fairly useful pattern in JavaScript. There is some added syntactic sugar around creating classes, but it isn't anything we haven't already seen in a previous chapter. The only warning I would give is that the template method uses inheritance and thus strongly couples the inherited classes with the parent class. This is generally not a desirable state of affairs.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset