The Template Method Pattern

Template pattern comes under behavior pattern group, as it’s used to manage algorithms, relationships and responsibilities between objects. This is an easy Pattern to understand, Let’s dig in,

In Template pattern, an abstract base class exposes defined templates to execute its methods. Its ConcreteClasses can override the method implementation as per need but without changing the algorithm’s structure and invocation.

So we can say, In Base class we declares algorithm ‘placeholders’, and ConcreteClasses implement the placeholders.

This particular design pattern is very useful , to avoid code duplication.By suing this pattern you let subclasses implement the behavior through overriding parent class algorithm.

there are four different types of methods used in the parent class:

Abstract methods
Method that are declared without any body within an abstract class is known as abstract method. The method body will be defined by its subclass. Any class that extends an abstract class must implement all the abstract methods declared by the super class.

Concrete methods
This methods are usually utility methods.These are standard complete methods that are useful to the subclasses.

Hook methods
Methods containing a default implementation that may be overridden in some classes. Hook methods are intended to be overridden.

Template methods
A method that calls any of the methods listed above in order to describe the algorithm without needing to implement the details.

Template Method Pattern Example

I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.