Learn the Template Method Design Pattern
This is the 14th post in a series on design patterns.
Template Method is a behavioral design pattern that defines the skeleton of an algorithm in the superclass but lets subclasses override specific steps of the algorithm without changing its structure.
Assume you are building an application that extracts data from documents. Users input documents in different formats (CSV, DOC, PDF) and try to extract meaningful data in a uniform format from these documents.
The first version of the application only works with CSV files. The following version was able to support DOC format and later could extract from PDF files. It occurred to you at some point that the three classes share a great deal of code. Although the code for converting various data formats was entirely different in all classes, the code for processing and analyzing the data was almost identical.
Template Method Design Pattern
Using the Template Method pattern, you break an algorithm into steps, turn those steps into methods, and put a series of calls to these methods within a single template method. The steps may either be abstract or have default implementations. The client must provide its own subclass, implement all abstract steps, and override some of the optional ones if needed, but should not override the template method.
UML Class Diagram
Not familiar with UML Class Diagram? I have written a detailed post on the UML Class diagram.
Implementation steps
- Analyze the target algorithm to determine if it can be broken down into steps. Consider which steps are common to all subclasses and which ones will remain unique.
- Declare the template method and a set of abstract methods that represent the algorithm’s steps in the abstract base class. Perform the corresponding steps in the template method to outline the algorithm’s structure. Make the template method final to prevent subclasses from overriding it.
- It’s fine if all steps are abstract. However, some steps may benefit from a default implementation. Subclasses do not need to implement these methods.
- Consider adding hooks between critical steps.
- Create a concrete subclass for each variation of the algorithm. It must implement all of the abstract steps, but may also override some of the optional ones.
Source Code Implementation
The DataExtractor abstract Class declares the steps of an algorithm, as well as the actual template method calling these steps in a particular order. These steps may either be abstract or have some default implementation.
CSVExtractor concrete classes can override all the steps, but not the template method itself.
PDFExtractor concrete classes can override all of the steps, but not the template method.
The TemplateMethodClient uses the template method.
When To Apply Template Method Design Pattern
- Template Methods are useful for letting clients extend only certain steps of an algorithm, but not the whole algorithm or its structure.
- Whenever you have several classes that contain almost identical algorithms with a few minor differences, use the pattern. As a result, you may need to modify both classes when the algorithm changes.
Pros of Template Method Design Pattern
- You can let clients override only certain parts of a large algorithm, allowing them to be less affected by changes to other parts.
- The duplicate code can be pulled into the superclass.