Archive

Template method design pattern falls under the category of Behavioral Design Pattern. In this pattern, a template method defines a skeleton of an algorithm in terms of abstract operations. The template method can contain one or more steps. But these steps will have to be in abstract form only. That said, we cannot change the order of steps, and most importantly we cannot override the template method itself. Only the steps given in the skeleton of algorithm of template method need to be overridden in concrete classes.

We see the concrete classes are overriding the abstract operations defined by the template method in its algorithm. This way template method pattern provides an abstract view of algorithm.

So in practical scenario, this pattern fits only when different types of object instances are required to invoke methods or operations that differ sharply in implementation but the algorithm remaining same. Also, sometimes when are refactoring multiple classes, we can find template method pattern coming into picture.

Important point to note here is: the way we are calling TemplateMethod() of ConcreteClassA from base class AbstractAlgorithmSkeleton reminds us of “The Hollywood Principle”- “Do not call us, we will call for you”. That is, child class method is being called from base class. This way of method call is also known as Inversion of Control.