GoF Definition: Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. The template method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.
In a template method, we define the minimum or essential structure of an algorithm. Then we defer some functionalities (responsibilities) to the subclasses. As a result, we can redefine certain steps of an algorithm by keeping the key structure fixed for that algorithm.
Suppose we want to make pizza. The basic mechanism is the same, but extra materials are added based upon the customer’s choice—whether he/she wants a vegetarian pizza or a non-vegetarian pizza.
For an engineering student, in general, most of the subjects in the first semester are common for all concentrations. Later, additional papers are added in his/her course based on his/her specialization (Computer Science, Electronics, etc.).
Here we have tried to implement our example with a similar concept. The parts of the program are described by the following structure in the solution. We have implemented a simple program to design engineering courses to illustrate the template method pattern. We are assuming that all engineering students need to pass mathematics and soft skills in their initial semesters to obtain their final degree. Later, some special papers will be added to their course based on their concentration (e.g., computer science or electronics).
High-level structure of the parts of the program is as follows:
// BasicEngineering.java
package engineering.papers;
public abstract class BasicEngineering
{
// Papers() in the template method
public void Papers()
{
//Common Papers:
Math();
SoftSkills();
//Specialized Paper:
SpecialPaper();
}
//Non-Abstract method Math(), SoftSkills() are //already implemented by Template class
private void Math()
{
System.out.println("Mathematics");
}
private void SoftSkills()
{
System.out.println("SoftSkills");
}
//Abstract method SpecialPaper() must be implemented in derived classes
public abstract void SpecialPaper();
}
// ComputerScience.java
package engineering.papers;
public class ComputerScience extends BasicEngineering
{
@Override
public void SpecialPaper()
{
System.out.println("Object Oriented Programming");
}
}
// Electronics.java
package engineering.papers;
public class Electronics extends BasicEngineering
{
@Override
public void SpecialPaper()
{
System.out.println("Digital Logic and Circuit Theory");
}
}
// TemplateMethodPatternEx.java
package template.method.pattern.Demo;
import engineeringPapers.*;
class TemplateMethodPatternEx
{
public static void main(String[] args)
{
System.out.println("***Template Method Pattern Demo*** ");
BasicEngineering bs = new ComputerScience();
System.out.println("Computer Sc Papers:");
bs.Papers();
System.out.println();
bs = new Electronics();
System.out.println("Electronics Papers:");
bs.Papers();
}
}
What is the major precaution we should take for implementing this method?
We need to minimize the number of incomplete/abstract operations. (In Java, remember: an abstract method does not have a body). Otherwise, each of the subclasses needs to override them and the overall process will lose the effectiveness of this design pattern.
18.191.235.8