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.

abstract template class

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:

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;
    }
    @Override
    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.

#java #design-pattern #programming #developer

Using Template Method Design Pattern In Java
2.30 GEEK