The Template Method Pattern in Java

Introduction to the Template Method Pattern

Design pattern in a simple meaning, is a way to design reusable object-oriented code. We can think of ways to design our classes, interfaces, enums and their members so that the code is reusable but flexible.

There are 23 most important design patterns, pioneered by the book Design Patterns: Elements of Reusable Object-Oriented Software, written by Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides or Gang of Four for short. We will cover these design pattern one by one.

By the original GoF definition, the Template Method pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.

Implementation of Template Method pattern in Java

This pattern seems complex right? In fact the Template Method pattern is one of the easiest. Say we have to design a slide – template with three parts, header, footer and content. The content changes in each slide but the header and footer normally stay the same. So we create an interface to define these parts. In Template.java:

public abstract class Template {
  protected void showHeader(){
    System.out.println("Header");
  }
  protected void showContent(){
    System.out.println("Content");
  }
  protected void showFooter(){
    System.out.println("Footer");
  }
  public final void showPage(){
    showHeader();
    showContent();
    showFooter();
  }
}

We can see that the Template defers some operations to subclasses. And the subclass of Template can choose to keep the parent’s methods or overriding them. For example, we create a new Template concrete class, in the src folder we create Page01.java:

public class Page01 extends Template{
  protected void showContent() {
    System.out.println("This is content 01");
  }
}

We can see that the protected methods showContent(), showHeader() and showFooter() can be a placeholder for the subclass to override. And such they are sometimes called hooks.

We can also create a Page02 class in which we modify the Header and Footer:

public class Page02 extends Template{
  protected void showHeader() {
    System.out.println("Header 02");
  }
  protected void showContent() {
    System.out.println("This is content 02");
  }
  protected void showFooter(){
    System.out.println("Footer 02");
  }
}

Now in Test.java:

public class Test {
  public static void main(String[] args){
    Template page01 = new Page01();
    page01.showPage();
    System.out.println("---------------------");
    Template page02 = new Page02();
    page02.showPage();
  }
}

The output will be:

Header
This is content 01
Footer
---------------------
Header 02
This is content 02
Footer 02

Note that the showPage() method is final, so the order of Header, Content and Footer is fixed. Now we can see that the template pattern is rather easy to understand and to apply to our projects.

Leave a Reply