Open In App

Template Method Design Pattern in Java

Template Design Pattern or Template Method is the behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.

This pattern falls under the category of the “behavioral” design patterns as it is concerned with how classes collaborate and communicate with other classes.



Key Component of Template Method Design Pattern

In Java, the Template Method pattern is implemented using abstract classes. Let’s see the key elements of the Template Method Pattern:



Abstract Class

Concrete Classes

Template Method Invocation

Hooks

So this Pattern provides a way to define the skeleton of an algorithm in the superclass while allowing subclasses to provide specific implementations for certain steps. This promotes code reuse and ensures that the overall algorithm structure remains consistent across different implementations.

Example of Template Method Design Pattern in Java

Problem Statement:

Let’s assume we are developing a game that involves different characters (e.g., warriors, mages, archers) that have a common sequence of actions during their turn in a battle. However, each character may have specific behavior for certain actions like attacking, defending, and resting.

Explanation of the Problem:

So in this problem, the ‘Character’ class is the abstract class that defines the template method ‘takeTurn()’. The concrete subclasses (‘Warrior’ and ‘Mage’) implement the abstract methods (‘startTurn()’, ‘performAction()’, ‘endTurn()’) according to their specific behaviors. The client code (‘Game’ class) can create instances of different characters and invoke their ‘takeTurn()’ method, relying on the template method to ensure a common sequence of actions.

Overall Code of above problem Statement:




// Abstract class representing the character
abstract class Character {
     
    // Template method that defines the common sequence of actions
    public void takeTurn() {
        startTurn();
        performAction();
        endTurn();
    }
     
    // Abstract methods to be implemented by subclasses
    protected abstract void startTurn();
     
    protected abstract void performAction();
     
    protected abstract void endTurn();
}
 
// Concrete subclass representing a warrior character
class Warrior extends Character {
     
    @Override
    protected void startTurn() {
        System.out.println("Warrior is preparing for the turn.");
    }
 
    @Override
    protected void performAction() {
        System.out.println("Warrior is attacking with a sword.");
    }
 
    @Override
    protected void endTurn() {
        System.out.println("Warrior is resting after the turn.");
    }
}
 
// Concrete subclass representing a mage character
class Mage extends Character {
     
    @Override
    protected void startTurn() {
        System.out.println("Mage is focusing energy for the turn.");
    }
 
    @Override
    protected void performAction() {
        System.out.println("Mage is casting a spell.");
    }
 
    @Override
    protected void endTurn() {
        System.out.println("Mage is recovering after the turn.");
    }
}
 
// Client code
public class Game {
    public static void main(String[] args) {
        // Creating instances of different characters
        Character warrior = new Warrior();
        Character mage = new Mage();
         
        // Invoking the template method for each character
        System.out.println("Warrior's Turn:");
        warrior.takeTurn();
         
        System.out.println("\nMage's Turn:");
        mage.takeTurn();
    }
}

Output
Warrior's Turn:
Warrior is preparing for the turn.
Warrior is attacking with a sword.
Warrior is resting after the turn.

Mage's Turn:
Mage is focusing energy for the turn.
Mage is casting a spell.
Ma...

Diagrammatic Representation of Above Problem

Working of Template Method pattern

Abstract Character Class




abstract class Character {
     
    // Template method that defines the common sequence of actions
    public void takeTurn() {
        startTurn();
        performAction();
        endTurn();
    }
     
    // Abstract methods to be implemented by subclasses
    protected abstract void startTurn();
     
    protected abstract void performAction();
     
    protected abstract void endTurn();
}

Concrete Warrior Class




class Warrior extends Character {
     
    @Override
    protected void startTurn() {
        System.out.println("Warrior is preparing for the turn.");
    }
 
    @Override
    protected void performAction() {
        System.out.println("Warrior is attacking with a sword.");
    }
 
    @Override
    protected void endTurn() {
        System.out.println("Warrior is resting after the turn.");
    }
}

Concrete Mage Class




class Mage extends Character {
     
    @Override
    protected void startTurn() {
        System.out.println("Mage is focusing energy for the turn.");
    }
 
    @Override
    protected void performAction() {
        System.out.println("Mage is casting a spell.");
    }
 
    @Override
    protected void endTurn() {
        System.out.println("Mage is recovering after the turn.");
    }
}

Client Code (Game Class)




public class Game {
    public static void main(String[] args) {
        // Creating instances of different characters
        Character warrior = new Warrior();
        Character mage = new Mage();
         
        // Invoking the template method for each character
        System.out.println("Warrior's Turn:");
        warrior.takeTurn();
         
        System.out.println("\nMage's Turn:");
        mage.takeTurn();
    }
}

Use Cases of Template Method Design Pattern in Java

This pattern is quite useful in scenarios where we have a common algorithm that needs slight variations in its steps based on different contexts. let’s see some of the common use cases of the Template Method Pattern:

Advantages of Template Method Design Pattern in Java

Let’s see some of the advantages of Template Method Pattern:

Disadvantages of Template Method Design Patten in Java

While the Template Method Pattern offers several advantages, it also has some potential disadvantages also:


Article Tags :