The Template Method design pattern is part of the so-called behaviour pattern family. These patterns help solve problems of interaction between classes and objects. This pattern arises from the need to extend certain behaviours within the same algorithm by different entities. That is, different entities have a similar behaviour but differ in certain specific aspects depending on the particular entity.
One possible solution could be to copy the algorithm in each of the different entities by changing the specific part in which they differ. This solution has a negative consequence since duplicate code is generated. The solution proposed by the Template design pattern is to abstract all the behaviours that entities share in a (abstract) class from which, later on, they will extend those entities. This superclass will define a method that will contain the skeleton of that common algorithm (template method) and delegate a certain responsibility in the child classes, through one or several abstract methods that must be implemented.
A real-world example
Let’s say we want to make breakfast. To do so we need to follow the following general steps:
- Prepare Ingredients
- Cook Ingredients
- Serve Food
Depending on what type of breakfast we want to prepare the cooking process might be different, we could have a light and a heavy breakfast. We will assume that the preparation of ingredients and serve food steps are the same for making light or heavy breakfast. Let’s convert the preceding real-world example into technical code.
We will be dividing the system into the following components for our implementation of this pattern:
- Abstract Class: This will be an abstract class, that will have an abstract method of cook ingredients, that will be implemented in the sub-classes based on type of breakfast and the non-abstract methods of Prepare Ingredients and Serve FoodMost importantly, this class will contain a Non Abstract method that will be calling the abstract methods or any other method defined in this class, to outline the flow of the overall process in a step-wise call to these methods, or you can say, it will contain the sequence of an algorithm.
- Concrete Classes: These will be concrete implementations of our abstract step of Cook Ingredients, depending on the type of breakfast.
So our initial code setup for defining the template of the make breakfast process will be like the following:
We will next be implementing the template, to define the cook ingredients, based on the type of breakfast as in the following:
So now we have set up the complete process of making a breakfast for the two different types, with a few common steps. So we begin the process with the following code implementation:
So this was all about the Template Method Pattern. You can find the code HERE
Example in ASP.NET Page Life Cycle
One scenario where we see the Template pattern very much visible is in the ASP.NET page life cycle. In the ASP.NET page life cycle, the life cycle sequence is fixed but it provides full authority to override the implementation for each sequence.
For instance, in the ASP.NET page life cycle, we have various events like Init, Load, Validate, Prerender, Render, etc. The sequence is fixed, but we can override the implementation for each of these events as per our needs.