This is one of the variations of behavioral design patterns. A Template Pattern "just defines the skeleton of a function in operation, deferring some steps to its subclasses". The skeleton of the algorithm is defined by The template design. The skeleton forms the operation's basic structure, which is divided into various subclasses to operate. The details will be available to the child classes. When the implementation is done, the child class will cover all the details. The main structure formation will be done by considering the parent class as the main object, and the whole structure formation will be done by the same plan. The parent class consists of the main methods, and the rest are given to the child classes. The template has a fixed format. As we know about HTML(hypertext markup language ), the representation of the template remains fixed in all circumstances. The template design pattern consists of many steps, and the steps are the backbone of the overall system because they are responsible for the execution of the whole system. Maintaining the steps carefully and effectively implementing them is important. The steps will follow the abstract method to implement the subclasses. The template design pattern is one of the easiest design patterns to understand and implement among all the available design patterns under the category of behavioral design patterns. The most popular use of it is in framework development; most framework developers use template design patterns. The code duplication problem is also solved here. No code duplication is allowed in the template design pattern. In the Template design pattern, we have to expose the abstract class to execute all its available methods. This method's implementation can be overridden as per need, but we must ensure that the invocation will be the same as defined by an abstract class. This design pattern has some disparities from others because no other design pattern is repeatedly used when they are implemented in concrete class. Still, when it is implemented in concrete classes, it is used multiple times in the template design pattern.
Problem statement :
Suppose you are a software engineer and are supposed to make one project. The project's main feature is that it should be able to extract data from the document submitted by the user. The data needs to be extracted in a specific format, filtered, and delivered to the backend. So after working on the projects for some weeks, you were able to deliver the project. Now the client is very happy and impressed by the project, so he wants to add some more features to it. Previously your project was only able to extract the data from the dc and CSV file, but now the client wants that it should extract data from the pdf too. So you started working and finally completed the task. Still, while checking the code, you realized that the code for extracting the data from both formats is the same, and the code to filter the text is also the same. This might seem like a code duplication problem. As an experienced software engineer, you know that practising code duplication is not a good practice, so what should be done in this scenario?
This problem can be solved easily with a template design pattern. To solve this code duplication, we first need to break the algorithm into various steps, and then the methods will be implemented through these steps. We will put all the methods inside the template class and have to do some default implementation for handling extraordinary situations. We know some specific methods of some specific format is different, so there is no point in touching those special methods. Still, the methods of analyzing the same data and some other methods are the same s we will be implementing the skeleton for that, and then we will do a step-wise division of the algorithm, which will be pulled to the base class. There is one more method, and it is called the hook. It is for analyzing the empty body of a document, so hooks are used before and after all the important implementation methods.