The Template Method Design Pattern
The Template Method pattern defines the steps of an algorithm and allows subclasses to provide the implementation for one or more steps.
Use the Template Method pattern when you want to define the skeleton of an algorithm, letting the subclasses fill in the steps. This is especially useful when the algorithm has invariant steps that are the same across all implementations.
- TypeBehavioral
- Time ComplexityO(1) Constant Time – The algorithm runs in constant time, irrespective of the input size. Operations like accessing an array element fall into this category.
- Efficiency7
-
Learning Effort
- Average4
- Conceptual4
- Debug and Maintain4
- Implementation3
- Prerequisites3
- Versatility5
Things you should know before you start
- Inheritance Understanding of inheritance is essential, as the Template Method pattern relies on subclassing.
Minimal Example
Implementing a data mining operation where the steps of data collection and analysis are the same, but the data parsing method varies.
abstract class DataMiner { abstract parseData(): void; collectData(): void { /* ... */ } analyzeData(): void { /* ... */ } } const miner = new PDFDataMiner(); miner.collectData(); miner.parseData(); miner.analyzeData();
What’s happening in thie example
In this example, the `DataMiner` serves as the AbstractClass, and `PDFDataMiner` is a ConcreteClass. The `clientCode` shows how to use the Template Method to perform a data mining operation.
How the Template Method Works
The Template Method pattern helps to ensure that the algorithm’s structure stays the same, while subclasses provide some parts of the implementation.
This pattern is useful for code reuse and encapsulation of complex algorithms.
Entities in the Template Method Design Pattern
-
AbstractClassThe AbstractClass defines abstract primitive operations that concrete subclasses should implement. It also defines a template method that uses these operations.
- AbstractClass has a one-to-many extends relationship with ConcreteClass
What to watch out for when using or considering this design pattern.
Avoid using the Template Method pattern if you want to let clients replace not just the details of the algorithm, but the entire algorithm itself. Strategy would be a better fit in such cases.
Design patterns often confused with the Template Method Design Pattern
The following design patterns are often confused with the Template Method
for various resons.
If you’re exploring solutions, have a look through this list and see if
one of these might be a better fit for your problem.
-
Strategy – The Strategy pattern defines a family of algorithms and makes them interchangeable.
The Strategy pattern is similar to the Template Method in the following ways.
- Both patterns aim to encapsulate algorithmic details.
- Both can be used to implement a family of algorithms.
- Template Method uses inheritance to vary part of an algorithm, while Strategy uses composition to switch between algorithms.
-
Observer – The Observer pattern defines a one-to-many dependency between a subject and multiple observers.
The Observer pattern is similar to the Template Method in the following ways.
- Both patterns are behavioral design patterns.
- Both can be used to extend functionalities.
- Template Method is focused on algorithms and their steps, while Observer is focused on state changes and notifications.