Template Method Pattern


Java and most OO languages allow a child class to override individual methods in the parent class. Each of these methods may define an independent functionality. The Template Method pattern provides an orchestrated approach to this.

It is based on a method in the parent abstract class. This method sequentially calls several individual abstract methods within the parent class. The subclasses just need to provide an implementation to these abstract methods. The template method definition in the base class takes care of appropriately invoking these individual methods.

The Template Method Design Pattern has only two participants

  • The Abstract Template Class: This contains the templateMethod(). This method is defined as final - so that it cannot be overridden. All operations used by this template method are made abstract, so their implementation is deferred to subclasses.
  • Concrete Class: This implements all the operations required by the templateMethod that were defined as abstract in the parent class. There can be many different Concrete Classes