Thilan Dissanayaka Design Patterns Apr 26

Template Pattern explained simply

Ever found yourself writing similar logic over and over, only to change a few steps each time?
That’s exactly what the Template Pattern helps you solve.

The Template Pattern is a behavioral design pattern that defines the skeleton of an algorithm in a method, deferring some steps to subclasses. It lets you reuse algorithm structure while letting subclasses refine certain steps without changing the overall logic.

What is the Template Pattern?

At its core, the Template Pattern:

  • Defines the outline (template) of an algorithm.
  • Implements the invariant parts (that don’t change).
  • Leaves the changing parts (abstract steps) to be defined by subclasses.

Think of it like a recipe: the steps are fixed, but the ingredients may vary.

Real-Life Analogy

Imagine you’re making a cup of tea or coffee. The process is almost the same:

  1. Boil water
  2. Brew drink
  3. Pour into cup
  4. Add condiments

The steps are identical, but "brew drink" and "add condiments" differ.

Template Pattern in Code (Java)

Let’s define an abstract class with a template method:

public abstract class Beverage {
    // Template method
    public final void prepareRecipe() {
        boilWater();
        brew();
        pourInCup();
        addCondiments();
    }

    private void boilWater() {
        System.out.println("Boiling water");
    }

    private void pourInCup() {
        System.out.println("Pouring into cup");
    }

    // Abstract methods to be implemented by subclasses
    protected abstract void brew();
    protected abstract void addCondiments();
}

Now let’s create two subclasses: **Tea** and **Coffee**.

```java
public class Tea extends Beverage {
    @Override
    protected void brew() {
        System.out.println("Steeping the tea");
    }

    @Override
    protected void addCondiments() {
        System.out.println("Adding lemon");
    }
}

```java
public class Coffee extends Beverage {
    @Override
    protected void brew() {
        System.out.println("Dripping coffee through filter");
    }

    @Override
    protected void addCondiments() {
        System.out.println("Adding sugar and milk");
    }
}

And use them like this:

```java
public class MainProgram {
    public static void main(String[] args) {
        Beverage tea = new Tea();
        tea.prepareRecipe();

        System.out.println();

        Beverage coffee = new Coffee();
        coffee.prepareRecipe();
    }
}

**Output:**

Boiling water Steeping the tea Pouring into cup Adding lemon

Boiling water Dripping coffee through filter Pouring into cup Adding sugar and milk



## Key Components

- **Abstract Class (`Beverage`)**: Contains the template method and defines abstract steps.
- **Template Method (`prepareRecipe`)**: Defines the algorithm structure.
- **Concrete Classes (`Tea`, `Coffee`)**: Provide specific implementations for the abstract steps.

## When to Use the Template Pattern?

- When multiple classes share the same algorithm structure but differ in steps.
- When you want to avoid code duplication and increase reuse.
- When you want to control the algorithm’s structure tightly while allowing customization of parts.

---

## Advantages

✅ Promotes code reuse.  
✅ Ensures consistent algorithm structure.  
✅ Follows the Hollywood Principle: *“Don’t call us, we’ll call you.”* (i.e., the base class controls flow)

## Disadvantages

❌ Requires inheritance, which may reduce flexibility.  
❌ Can lead to class explosion if many subclasses are needed.  
❌ Harder to understand for beginners due to indirect control flow.

## Real-World Use Cases

- Frameworks that define **execution flows** (e.g., Spring’s `AbstractController` or `AbstractView`).
- Algorithms like **sorting**, **validation pipelines**, or **data import/export** workflows.
- UI frameworks that define base rendering logic but allow customization of widgets.

## Final Thoughts

The Template Pattern is perfect when you have an algorithm with a fixed structure, but with parts that vary. It helps you write **clean**, **reusable**, and **maintainable** code.

> Next time you find repeated logic that only changes in a few places — reach for the Template Pattern!

Happy templating! 🍵☕💻
ALSO READ
Reverse TCP shell with Metasploit
Mar 23 Web App Hacking

Metasploit is an awesome tool which is. It can automate the exploitation process, generate shellcodes, use it as a listener, etc. I hope to start a tutorial series on the Metasploit framework and its....

Abstract Factory Pattern explained simply
Apr 26 Design Patterns

When you want to create **families of related objects** without specifying their concrete classes, the **Abstract Factory Pattern** is your best friend. --- ## What is the Abstract Factory....

SQL injection login bypass
Apr 26 Web App Hacking

SQL Injection (SQLi) is one of the oldest and most fundamental web application vulnerabilities. While it’s becoming rarer in modern web apps due to better coding practices and frameworks,....

Termux command list
Apr 26 Uncategorized

Termux is a terminal emulator application for mobile devices. In this document, we are going to talk about Termux and its features. We can use it to install Linux tools on a mobile phone. Here you....

Tic-Tac-Toe Game with Atmega 256 MicroController
Mar 23 Software Architecture

In this blog, I’ll walk you through how I built a **Tic-Tac-Toe game** using an AVR microcontroller, a 4x3 keypad, and a grid of LEDs. This project combines the basics of embedded programming, game....

Template Pattern explained simply
Apr 26 Design Patterns

Ever found yourself writing similar logic over and over, only to change a few steps each time? That’s exactly what the **Template Pattern** helps you solve. The **Template Pattern** is a....