Using Template Method Design Pattern In Java
Today, I will discuss another very useful design pattern named the Template Method Design Pattern. Read on to find out more!
Today, I will discuss another very useful design pattern named the Template Method Design Pattern.
Template Method Design Pattern
- The Template Method pattern is one of the behavioral design patterns identified by Gamma et al. in the book Design Patterns.
- The Template Method pattern provides a method in a super-class, usually an abstract super-class, and defines the skeleton of an operation in terms of several high-level steps.
- Generally, these steps are implemented by additional helper methods in the same class as the template method.
- The helper methods may be either created as an abstract method, for which sub-classes are required to provide concrete implementations, or hook methods, which have empty bodies in the super-class.
- The Template Method design pattern is used to define an algorithm as a skeleton of operations and leave the details to be implemented by the child classes.
- In this way of implementation, the overall structure and sequence of the algorithm are preserved by the parent class.
- The Template Method pattern defines the sequential steps to execute a multi-step algorithm. We can provide a default implementation as well.
- In the Template Method pattern, we define a preset structure method called template method which consists of steps.
- These steps can be created as an abstract method which will be implemented by its sub-classes.
- In the Template Method pattern, an abstract class exposes defined way(s)/template(s) to execute its methods.
- The template method uses and defines the sequence of steps to perform the algorithm.

Let's take an example to understand it better.
Car Manufacturing Example Using Template Method Design Pattern
I am using the same example I used for explaining the Builder Design Pattern to help you understand the difference between both.
First, we define an abstract class to represent a car manufacturing template. Here's the code for CarTemplate class:
x
package org.trishinfotech.template;
public abstract class CarTemplate {
protected String chassis;
protected String body;
protected String paint;
protected String interior;
public CarTemplate() {
super();
}
// steps
public abstract void fixChassis();
public abstract void fixBody();
public abstract void paint();
public abstract void fixInterior();
// template method
public void manufactureCar() {
fixChassis();
fixBody();
paint();
fixInterior();
}
public String getChassis() {
return chassis;
}
public void setChassis(String chassis) {
this.chassis = chassis;
}
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getPaint() {
return paint;
}
public void setPaint(String paint) {
this.paint = paint;
}
public String getInterior() {
return interior;
}
public void setInterior(String interior) {
this.interior = interior;
}
public String toString() {
// StringBuilder class also uses Builder Design Pattern with implementation of
// java.lang.Appendable interface
StringBuilder builder = new StringBuilder();
builder.append("Car [chassis=").append(chassis).append(", body=").append(body).append(", paint=").append(paint)
.append(", interior=").append(interior).append("]");
return builder.toString();
}
}
Here please notice that manufactureCar() method is a template method here. And methods fixChassis(), fixBody(), paint() and fixInterior() are the steps which will be defined by different sub-classes. The template method uses and defines the sequence of steps to perform the algorithm.
Now we will implement concrete sub-classes to define different flavors of templates.
Here's the code for ClassicCar class:
xxxxxxxxxx
package org.trishinfotech.template;
public class ClassicCar extends CarTemplate {
public ClassicCar() {
super();
}
public void fixChassis() {
System.out.println("Assembling chassis of the classical model");
this.chassis = "Classic Chassis";
}
public void fixBody() {
System.out.println("Assembling body of the classical model");
this.body = "Classic Body";
}
public void paint() {
System.out.println("Painting body of the classical model");
this.paint = "Classic White Paint";
}
public void fixInterior() {
System.out.println("Setting up interior of the classical model");
this.interior = "Classic interior";
}
}
Here's the code for ModernCar class:
xxxxxxxxxx
package org.trishinfotech.template;
public class ModernCar extends CarTemplate {
public ModernCar() {
super();
}
public void fixChassis() {
System.out.println("Assembling chassis of the modern model");
this.chassis = "Modern Chassis";
}
public void fixBody() {
System.out.println("Assembling body of the modern model");
this.body = "Modern Body";
}
public void paint() {
System.out.println("Painting body of the modern model");
this.paint = "Modern Black Paint";
}
public void fixInterior() {
System.out.println("Setting up interior of the modern model");
this.interior = "Modern interior";
}
}
Here's the code for SportsCar class:
xxxxxxxxxx
package org.trishinfotech.template;
public class SportsCar extends CarTemplate {
public SportsCar() {
super();
}
public void fixChassis() {
System.out.println("Assembling chassis of the sports model");
this.chassis = "Sporty Chassis";
}
public void fixBody() {
System.out.println("Assembling body of the sports model");
this.body = "Sporty Body";
}
public void paint() {
System.out.println("Painting body of the sports model");
this.paint = "Sporty Torch Red Paint";
}
public void fixInterior() {
System.out.println("Setting up interior of the sports model");
this.interior = "Sporty interior";
}
}
Now we will write our Main class to execute and test the output:
xxxxxxxxxx
package org.trishinfotech.template;
public class Main {
public static void main(String[] args) {
CarTemplate car = new SportsCar();
car.manufactureCar();
if (car != null) {
System.out.println("Below car delievered: ");
System.out.println("======================================================================");
System.out.println(car);
System.out.println("======================================================================");
}
}
}
Below is the output:
xxxxxxxxxx
Assembling chassis of the sports model
Assembling body of the sports model
Painting body of the sports model
Setting up interior of the sports model
Below car delievered:
======================================================================
Car [chassis=Sporty Chassis, body=Sporty Body, paint=Sporty Torch Red Paint, interior=Sporty interior]
======================================================================
Well, there you have it! I hope this tutorial helped to understand the Template Method pattern.
Source Code can be found here: Template-Method-Design-Pattern-Source-Code
Liked the article? Please don't forget to press that like button. Happy coding!
Opinions expressed by DZone contributors are their own.