Template Design Pattern
Crime Fighting 101: Batman’s Guide to the Template Design Pattern!
Alright, buckle up! Today we’re diving into the exciting world of the Template Design Pattern. No need for a cape or fancy gadgets — just a good old Java example.
What is the Template Design Pattern?
Imagine Batman (yes, the caped crusader!) and Robin trying to solve a crime. Every time they go on a mission, Batman does his thing: investigate the scene, fight the bad guys, and deliver the one-liners. Robin does the same thing, but he’s still figuring out how to be a superhero. Now, Batman has a strict routine, but he’s generous enough to let Robin try it his way. That’s what the Template Design Pattern is!
Get Aradhana Singh’s stories in your inbox
Join Medium for free to get updates from this writer.
It’s like a blueprint for a series of steps that should be followed, but it allows specific parts to be customized by subclasses. So while the structure remains the same (because criminals are always criminals), the details (how Batman fights them, for instance) can change.
Batman’s Routine = Template
Batman has a fixed routine for crime-fighting, but there are places where he allows Robin to improvise. Here’s a breakdown of his template:
- Detect Crime — Batman’s always watching Gotham.
- Confront Villain — No villain escapes!
- Fight Villain — Batman has all sorts of cool moves.
- Deliver Dramatic Line — A classic Batman thing. “I am the night!”
- Save Gotham — Usually followed by a heroic exit.
Now lets look at Batman’s template:
abstract class Batcave {
// The Template Method - defines the skeleton of the crime-fighting process
public final void fightCrime() {
detectCrime();
confrontVillain();
fightVillain();
deliverLine();
saveGotham();
}
// Concrete Methods
private void detectCrime() {
System.out.println("Batman detects a crime in Gotham.");
}
private void confrontVillain() {
System.out.println("Batman confronts the villain in the shadows.");
}
private void deliverLine() {
System.out.println("Batman delivers a dramatic line: 'I am the night!'");
}
private void saveGotham() {
System.out.println("Batman saves Gotham. Again.");
}
// Abstract Method - this part is left to subclasses to define
protected abstract void fightVillain();
}
class Batman extends Batcave {
// Batman’s unique way of fighting villains
@Override
protected void fightVillain() {
System.out.println("Batman uses gadgets to fight the villain. BOOM! ZAP! POW!");
}
}
class Robin extends Batcave {
// Robin’s unique (and less effective) way of fighting villains
@Override
protected void fightVillain() {
System.out.println("Robin tries to fight the villain but mostly trips over his cape.");
}
}
public class Main {
public static void main(String[] args) {
Batcave batman = new Batman();
batman.fightCrime(); // Batman goes on a mission!
System.out.println("\n");
Batcave robin = new Robin();
robin.fightCrime(); // Robin attempts his first solo mission!
}
}What’s Happening Here?
Batcave(abstract class): This class has the overall structure of fighting crime, but it leaves the fighting part to the subclasses. The Template Method (fightCrime()) defines the general steps.fightVillain()(abstract method): This is where the subclasses (like Batman or Robin) get to add their own special touch—Batman uses gadgets, Robin... well, tries his best!- Batman and Robin: These two classes inherit from
Batcaveand implement thefightVillain()method differently. Batman is a seasoned pro, and Robin... let’s just say he’s a work in progress.
Why Should You Care?
The Template Design Pattern is useful when you have a series of actions that follow the same basic structure but need to be customized in some places. Just like Batman and Robin, they share the same template (crime-fighting), but how they fight is different.
Final Thoughts
So next time you see Batman fighting crime, remember — it’s all about templates. Whether you’re solving crimes in Gotham or solving problems in your code, having a solid template to follow can save you time and energy (and keep your cape from getting caught in the Batmobile). Keep it simple, and let your subclasses handle the details.
That’s all, folks! Now go ahead, fight some crime in your code, and remember — always use a template.






