What is the Template Method pattern?
Options
- A class that generates HTML templates for the views of an app
- A base class sets the algorithm skeleton; subclasses override steps (Correct option)
- A pattern that is used to copy objects without their classes
- A way to cache the results of calls to an expensive method
Answer
A base class defines the fixed skeleton of an algorithm and lets subclasses override specific steps
Why
The superclass has a method with the overall steps in a fixed order (the template), and some steps are abstract or overridable. Subclasses fill in the details without changing the order.
Example: DataProcessor.process() always does read, validate, transform, save; subclasses implement transform().
Spring's JdbcTemplate and RestTemplate follow a similar idea: they handle the boilerplate (connections, errors) and you supply the variable part.
Remember
Template Method = fixed algorithm skeleton, customizable steps.