System Design ( Day - 56 )

Template Design Pattern
Definition
Template design pattern design the skeleton of an algorithm, for an operation defining some steps to subclasses, Template method let subclasses redefine certain steps of an algorithm without changing the algorithm structure.
When you have an algorithm made of fixed steps—but some steps vary depending on the subclass—the Template Method Pattern is ideal. It defines the skeleton of an operation in a base class, and defers specific steps to subclasses.
🎯 The Problem
Say you’re building a generic Model Trainer pipeline that always needs to:
Load data
Preprocess it
Train the model
Evaluate performance
Save results
But the details of preprocessing, training, and evaluation differ between, for example, a Neural Network and a Decision Tree. You want to:
Avoid duplicating the overall pipeline
Encapsulate variable behavior in subclasses
🧩 Pattern Breakdown
1. Abstract Template
public abstract class ModelTrainer {
// template method: fixed skeleton, `final` so subclasses can’t override
public final void run(String path) {
load(path);
preprocess();
train();
evaluate();
save();
}
// primitive steps to be implemented by subclasses
protected abstract void load(String path);
protected abstract void preprocess();
protected abstract void train();
protected abstract void evaluate();
protected abstract void save();
}
2. Concrete Implementations
javaCopyEdit// Neural network trainer
public class NeuralNetworkTrainer extends ModelTrainer {
@Override protected void load(String path) { /* load tensors… */ }
@Override protected void preprocess() { /* normalize inputs… */ }
@Override protected void train() { /* backprop… */ }
@Override protected void evaluate() { /* compute accuracy… */ }
@Override protected void save() { /* serialize weights… */ }
}
// Decision tree trainer
public class DecisionTreeTrainer extends ModelTrainer {
@Override protected void load(String path) { /* load CSV… */ }
@Override protected void preprocess() { /* handle missing values… */ }
@Override protected void train() { /* build trees… */ }
@Override protected void evaluate() { /* calculate depth… */ }
@Override protected void save() { /* write model to disk… */ }
}
3. Client Usage
javaCopyEditpublic class Client {
public static void main(String[] args) {
ModelTrainer nnTrainer = new NeuralNetworkTrainer();
nnTrainer.run("data/images/");
ModelTrainer dtTrainer = new DecisionTreeTrainer();
dtTrainer.run("data/tabular/");
}
}
TemplateClass defines the
templateMethod()
with calls to primitive operations (step1()
,step2()
, …).ConcreteClassA/B override these primitive steps.
🌟 Why Use Template Method?
Code Reuse: Share the common algorithm skeleton in one place.
Extensibility: Let subclasses tweak behavior by overriding specific steps.
Control: Keep the overall workflow intact—
templateMethod()
isfinal
.
Real World Example
Payment systems like For sending the money the template would be like
1. Checking the Balance in the account.
2. debit the money from the sender
3. checking the receivers account
4. Credit the money into receivers account
5. Validating the payment
these steps are the similar for the Upi payment, debit card payment and credit card payment we can use this template and we can implement the methods as we want in the concrete classes.
Subscribe to my newsletter
Read articles from Manoj Kumar directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
