Repaso
EspaƱol
Design & PracticesIntermediateDP-46

What is the Template Method pattern?

Options

  1. A class that generates HTML templates for the views of an app
  2. A base class sets the algorithm skeleton; subclasses override steps (Correct option)
  3. A pattern that is used to copy objects without their classes
  4. 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.

Key terms

Practice this question in Repaso