Now is the time to discuss the Template method pattern. Like strategy pattern, this pattern is part of the behavioral design patterns. As the name suggests Template, we know a template is a kind of layout available for some kind of process, which we can modify as per our needs and use it. Similar is the case here. We have some sub-tasks to complete a big task. We have a template of these sub-tasks, which we can use as per our situation requirements. So technically speaking, we make a template class, with the sub-tasks as abstract and implement them, as per our needs, in different cases. But one thing remains common – the overall steps of completing the task or the algorithm on the whole i.e. step 1, then step 2, then step 3 etc., remains the same in their execution order.
Let’s discuss this concept in detail now.
What is Template method pattern ?
According to GoF’s definition, this pattern is :
Define the skeleton of an algorithm in an operation, deferring some steps to sub-classes. Template Method lets sub-classes redefine certain steps of an algorithm without changing the algorithm’s structure.
A real world example :
An organization goes to colleges and hire freshers. They have a number of steps defined for the process of hiring in the following order
Step 1 – Take a test in first round
Step 2 – Group discussion in second round
Step 3 – Technical interview
Step 4 – HR interview
They have a common process of group discussions & hr interview for all the departments, but different test & technical interview process, depending on, whether it is a Computer or electronics departments. So we create a hiring process template which can be used by the company for two different departments.
Let’s convert the above real world example into technical code:
We will be dividing the system into the following components for our implementation of this pattern :
1. AbstractClass : This is the abstract class to define the template for our hiring process, which is to be changed as per the department, from which students are to be hired. It will be having abstract methods which will be implemented according to the requirements and non-abstract methods, which will be common to all the classes.
In our case, this class will be having the abstract methods of our first round of test & technical interview rounds, which will be implemented in the sub-classes based on the departments and the non-abstract methods of the group discussion and the hr rounds.
Most importantly, this class will have a non-abstract method which will be calling the abstract methods and non-abstract methods defined in this class, in a flow or order, to execute the overall process or the algorithm of the process.
In our code, this will be the method named HireFreshers().
2. Concrete Classes : These will be the classes which will have the implementation of the abstract methods defined in above class or in other words, they will implement the abstract steps of the template, as per their requirements.
In our case, these will be concrete department classes and will implement the abstract steps of the tests & technical interviews classes, depending on which department they are.
So our initial code setup for defining the template of hiring process will be like the following :
Next, we will be implementing the abstract methods in the template, to define the test & technical interview rounds, based on the type of department as :
So now we have setup the complete process of hiring for two different departments, with few common steps. Now we just need to start with the interview process for both the departments, using the client code. So we begin the process with the following code implementation :
So here we just created the instance of the department classes and based on their types, the algorithm is executed with the underlined implementation of the methods. Now in future if we have to do it for any other department, we just need to implement the abstract methods as per their departments and rest of the hiring process remains the same.
So this was all about Template Method pattern.Follow @dotnettechie