Template Method Design Pattern

Hey 👋🏻, I am , a Software Engineer from India. I am interested in, write about, and develop (open source) software solutions for and with JavaScript, ReactJs. 📬 Get in touch
Twitter: https://x.com/SankalpHaritash Blog: https://sankalp-haritash.hashnode.dev/ LinkedIn: https://www.linkedin.com/in/sankalp-haritash/ GitHub: https://github.com/SankalpHaritash21
📧 Sign up for my newsletter: https://sankalp-haritash.hashnode.dev/newsletter
The Template Method design pattern is a fundamental behavioral pattern that defines the program skeleton of an algorithm in a method, deferring some steps to subclasses. It allows subclasses to redefine certain steps of an algorithm without changing the algorithm's structure. This approach provides a framework that dictates the sequence of steps to execute various tasks but lets subclasses implement the specific details of some steps.
Components of the Template Method Pattern
Abstract Class (Template): This class defines a template method setting up the structure of an algorithm. It also declares abstract operations that subclasses must implement to make use of the template method.
Concrete Classes: These classes implement the abstract operations to carry out subclass-specific steps of the algorithm.
Example: Template Method in Java
Let's consider a simple example where we have a software build process. The process can vary depending on whether the software is for Android or iOS, but the steps to compile, link, and test are often similar.
abstract class BuildSoftware {
// Template method
public final void build() {
compile();
link();
test();
}
// Concrete implementation
public void compile() {
System.out.println("Compiling source code");
}
// Concrete implementation
public void link() {
System.out.println("Linking necessary libraries");
}
// Abstract method to be implemented by subclasses
abstract void test();
// Concrete implementation
public void archive() {
System.out.println("Archiving the build");
}
}
class AndroidBuild extends BuildSoftware {
@Override
void test() {
System.out.println("Running Android tests");
}
}
class IOSBuild extends BuildSoftware {
@Override
void test() {
System.out.println("Running iOS tests");
}
}
public class Main {
public static void main(String[] args) {
BuildSoftware androidBuild = new AndroidBuild();
androidBuild.build();
BuildSoftware iosBuild = new IOSBuild();
iosBuild.build();
}
}
In this example:
The
BuildSoftwareclass defines the skeleton of the build sequence.The
testmethod is abstract and must be implemented by subclasses to provide specific testing logic for different platforms.AndroidBuildandIOSBuildare concrete classes that implement thetestmethod specifically tailored for each platform.
Pros of Template Method Pattern
Promotes Reusability: The template method allows core algorithm steps to be reused while varying the details in subclasses.
Enforces a Structure: It enforces a predefined sequence of steps, ensuring that the overall algorithm follows a certain order.
Inversion of Control: It hands over the control of the steps to the superclass but leaves room for customization.
Cons of Template Method Pattern
Limiting Flexibility: By defining a specific sequence, it can limit the flexibility of the subclasses if they need a different step ordering.
Inheritance Overuse: The pattern uses inheritance for customization, which can lead to deeper inheritance hierarchies that might complicate the system design and increase maintenance costs.
Difficulty in Changes: Changes in the template method might require changes to all subclasses, especially if the step sequence or the method signatures are changed.
The Template Method pattern is a powerful tool for creating a framework that dictates the program flow and sequence but allows for customization in certain steps. It’s particularly useful when multiple classes are expected to execute similar methods in a specific order, yet some of those steps might require different implementations. This pattern promotes code reuse, maintains control over the sequence of operations, and supports the principle of inversion of control by decoupling the steps of an algorithm from the actual implementation of those steps.
Conclusion:
If you found this blog post helpful, please consider sharing it with others who might benefit. Follow me for more insightful content on JavaScript, React, and other web development topics.
Connect with me on Twitter, LinkedIn, and GitHub for updates and more discussions.
Thank you for reading! 😊





