The template method pattern is a simple pattern used when you need a general behavior but where the details of the algorithm must be specific to subclasses.
The template method pattern isolates various parts of an algorithm. The algorithm skeleton is defined in an abstract class where some steps of the algorithm are delegated to its subclasses and some others are fixed in the abstract class itself and cannot be overridden in subclasses.
The participants of this pattern are as follows:
The
AbstractClass
, which defines the template method, and the signature of the sub parts of the algorithm are invoked by the template method.The
ConcreteClass
implements abstract methods used by the template method of theAbstractClass
. It is possible to have several concrete classes.