00:00

Template Design Pattern

The Template Design Pattern is a behavioral design pattern. It is used when we want to define the overall structure (steps) of an algorithm in one place, but allow subclasses to change some specific steps without changing the main flow.

In simple words, this pattern creates a fixed template for an operation, and lets child classes fill in the details.


Real-Life Example

Think about making tea and coffee:

  • Boil water
  • Add ingredients
  • Pour into a cup
  • Add extra items (milk, sugar, lemon, etc.)

The steps are mostly the same, but some steps change based on whether we are making tea or coffee. This is exactly where the Template Design Pattern is useful.


When to Use Template Design Pattern

  • When multiple classes follow the same process but differ in some steps
  • When you want to avoid duplicate code
  • When the algorithm structure should not be changed by subclasses

Key Components

  • Abstract Class – Defines the template method and common steps
  • Template Method – A final method that defines the algorithm structure
  • Abstract Methods – Steps that subclasses must implement

Java Example of Template Design Pattern

Step 1: Create an Abstract Class


    abstract class Beverage {
    
        // Template method (final so subclasses cannot change the flow)
        public final void prepareBeverage() {
            boilWater();
            addMainIngredient();
            pourInCup();
            addExtras();
        }
    
        protected void boilWater() {
            System.out.println("Boiling water");
        }
    
        protected void pourInCup() {
            System.out.println("Pouring into cup");
        }
    
        // Steps to be implemented by subclasses
        protected abstract void addMainIngredient();
        protected abstract void addExtras();
    }
    

Step 2: Create Tea Class


    class Tea extends Beverage {
    
        @Override
        protected void addMainIngredient() {
            System.out.println("Adding tea leaves");
        }
    
        @Override
        protected void addExtras() {
            System.out.println("Adding lemon");
        }
    }
    

Step 3: Create Coffee Class


    class Coffee extends Beverage {
    
        @Override
        protected void addMainIngredient() {
            System.out.println("Adding coffee powder");
        }
    
        @Override
        protected void addExtras() {
            System.out.println("Adding milk and sugar");
        }
    }
    

Step 4: Test the Code


    public class TemplatePatternDemo {
    
        public static void main(String[] args) {
    
            Beverage tea = new Tea();
            tea.prepareBeverage();
    
            System.out.println("------------");
    
            Beverage coffee = new Coffee();
            coffee.prepareBeverage();
        }
    }
    

Output

    Boiling water
    Adding tea leaves
    Pouring into cup
    Adding lemon
    ------------
    Boiling water
    Adding coffee powder
    Pouring into cup
    Adding milk and sugar
    

Advantages of Template Design Pattern

  • Promotes code reuse
  • Makes code easier to maintain
  • Enforces a standard algorithm structure
  • Follows the Open/Closed Principle

Disadvantages

  • Can be difficult to understand for beginners
  • Too many abstract methods can make the design complex

Conclusion

The Template Design Pattern is very useful when you have a common workflow with small variations. It keeps the main algorithm safe while giving flexibility to subclasses. In Java applications, this pattern is widely used in frameworks and libraries to provide a standard structure with customizable behavior.

If you want clean, reusable, and well-structured code, the Template Design Pattern is definitely worth learning and using.