Overview
The Template Method Pattern is a behavioral design pattern that defines the skeleton of an algorithm in a base class but allows subclasses to provide specific implementations for certain steps. This pattern promotes code reuse and ensures the overarching structure of the algorithm remains unchanged.
Key Participants
Abstract Class
Defines the template method, which contains the skeleton of the algorithm.
Implements some steps of the algorithm and declares abstract methods for steps to be implemented by subclasses.
Concrete Class
Implements the abstract methods declared in the abstract class.
Provides specific behavior for steps of the algorithm.
Implementation in Code
Example: Preparing Beverages
// Abstract Class
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();
}
// Concrete Class: Tea
class Tea extends Beverage {
@Override
protected void brew() {
System.out.println("Steeping the tea");
}
@Override
protected void addCondiments() {
System.out.println("Adding lemon");
}
}
// Concrete Class: Coffee
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");
}
}
// Client
public class TemplateMethodPatternDemo {
public static void main(String[] args) {
Beverage tea = new Tea();
System.out.println("Preparing tea:");
tea.prepareRecipe();
System.out.println("\nPreparing coffee:");
Beverage coffee = new Coffee();
coffee.prepareRecipe();
}
}
Key Methods
templateMethod()
Defines the fixed structure of the algorithm.
Calls concrete or abstract methods implemented by subclasses.
Concrete and Abstract Methods
Subclasses override abstract methods to provide specific behavior for certain steps.
Advantages
Code Reuse: The template method defines common steps in one place, reducing code duplication.
Consistency: Ensures that the algorithm’s structure remains consistent across different implementations.
Flexibility: Subclasses can customize specific steps without altering the overall algorithm.
Disadvantages
Restricted Flexibility: The overall structure of the algorithm cannot be changed by subclasses.
Dependency on Abstract Class: Subclasses must follow the structure defined by the base class, which may lead to rigidity.
Real-World Examples
Frameworks: Frameworks use the template method to enforce a specific workflow while allowing developers to override specific steps.
Data Processing: Algorithms for reading, processing, and saving data can use the template method to define the sequence of operations.
Game Development: Game engines use the template method to define the lifecycle of a game (e.g., initialize, update, render, terminate).
When to Use the Template Method Pattern?
When multiple classes share a common structure in their algorithms but require variations in some steps.
When you want to enforce a consistent structure for an algorithm across different implementations.
When code reuse and reducing duplication are essential.