Software Engineering

Template Method Pattern

Introduction

The template method pattern is behavioral design pattern that defines the steps for an algorithm and allows sub-classes to provide custom implementation for one or more steps. This design pattern helps us to maximize the code re-usability.

General idea is that we identify common behavior among classes and then create abstract class containing all common code to avoid duplication. Sub-classes implement abstract class and they are allowed to redefine some methods that do not change algorithmic structure.

Definition and structure

Official definition of Template method pattern by GOF is:

Defines the skeleton of an algorithm in a method, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithms structure.

Let’s take a look at the structure:

template-method

As you can see from the image above, we have an abstract class that has one template method and two primitive (abstract) methods that concrete class implement.

Implementation

We are going to create a simple data parse application that does dummy parsing of some data. Of course our intent here is to understand this pattern and not to create a real world example of how to parse data.

Let’s say that our requirements are to create application that parse JSON and XML data.

Step 1

Create an abstract class that will implement our template method, abstract methods and one method that will be the same for all subclasses.

public abstract class DataParser {

    public void parseData(){

        readData();

        processData();

        displayDataToUser();

    }

    protected abstract void readData();

    protected abstract void processData();

    public void displayDataToUser(){
        System.out.println("We are displaying data to the user");
    }
}

Step 2

We are going to create concrete classes that implement our abstract class. Those concrete classes are allowed to change abstract methods from our abstract class.

public class JsonParser extends DataParser{

    @Override
    protected void readData() {
         System.out.println("We are reading JSON data.");
    }

    @Override
    protected void processData() {
         System.out.println("We are processing JSON data.");
    }    
}

Now lets create dummy XML parser class.

public class XmlParser extends DataParser{

    @Override
    protected void readData() {
         System.out.println("We are reading XML data.");
    }

    @Override
    protected void processData() {
         System.out.println("We are processing XML data.");
    }    
}

Step 3

So, now we need to create main class where we will call our data parsers.

public class TemplateMethodPatternDemo {

    public static void main(String[] args) {        
        JsonParser jsonParser = new JsonParser();
        jsonParser.parseData();
        System.out.println("---------------------");
        XmlParser xmlParser = new XmlParser();
        xmlParser.parseData();       
        }    
}

Step 4

Since we have finished our application, let’s run it to see the result.

We are reading JSON data.
We are processing JSON data.
We are displaying data to the user
---------------------
We are reading XML data.
We are processing XML data.
We are displaying data to the user

 

 

You Might Also Like

No Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.