Simple Explanation for Template Method Pattern
The Scenario: One Data Processing Service
You have a DataProcessingService
that handles customer data. Initially, you just validate the data and save it.
processCustomerData(customerData);
Then Requirements Keep Coming
- Validate data
- Clean data
- Encrypt fields
- Save to DB
- Send Email
- Log Transaction
And you realize you need different types of data processing:
CustomerData
processing (what you have)OrderData
processing (similar steps, but different validation rules)ProductData
processing (same flow, but different encryption requirements)
What’s the Problem?
- You’re copy-pasting the same processing flow everywhere
- Each data type has slightly different implementations for the same steps
- Adding a new step means updating multiple places
- The overall algorithm structure is repeated but the details vary
How Template Method Pattern Saves You
Template Method says:
Define the skeleton of your algorithm once. Let subclasses fill in the specific details.
So you:
- Create an abstract base class with the template method
- Define the step-by-step algorithm in the base class
- Make specific steps abstract so subclasses implement them
- Each data type extends the base class and provides its own implementation
Implementation
public abstract class DataProcessor {
// The template method - final so subclasses can't change the order
public final void process() {
validate();
clean();
encrypt();
save();
hook(); // optional step
}
protected abstract void validate();
protected abstract void clean();
protected abstract void encrypt();
protected abstract void save();
// Hook - optional extension point
protected void hook() {}
}
public class CustomerDataProcessor extends DataProcessor {
@Override
protected void validate() {
System.out.println("Validating customer data");
}
@Override
protected void clean() {
System.out.println("Cleaning customer data");
}
@Override
protected void encrypt() {
System.out.println("Encrypting customer data");
}
@Override
protected void save() {
System.out.println("Saving customer data to DB");
}
@Override
protected void hook() {
System.out.println("Sending welcome email");
}
}
Usage:
DataProcessor processor = new CustomerDataProcessor();
processor.process();
When to Use Template Method?
- When you have multiple classes with similar algorithms but different implementations
- When you want to avoid code duplication in the overall workflow structure
- When you need to control the extension points - limit where subclasses can make changes
- When most of your classes have similar characteristics but differ in specific details
Benefits
- Code Reuse: Write the algorithm structure once, reuse everywhere
- Consistency: All subclasses follow the same step sequence
- Flexibility: Easy to add new data types without changing existing code
- Control: The template method is
final
- subclasses can’t mess with the algorithm flow
Real-World Examples
- Framework development (framework defines structure, you fill in specifics)
- House construction (same steps: foundation, walls, roof - different materials)
- Daily routines (wake up, work, eat, sleep - different details)
- Data processing pipelines (same flow, different data formats)