The Template method pattern
The Template method pattern is used to define a basic template of an algorithm and have subclasses override specific steps at runtime. This means that you have a series of steps in an algorithm but you want to consider having placeholder calls that delegate the logic to subclasses. Depending on the result, the algorithm behaves differently, which means that this pattern leverages inheritance to provide specialization.
You mainly want to employ this pattern because it can get very repetitive to create similar methods that perform the same operation, such as checking a specific state variable, but differ in some aspects. Let's explain in detail when to use this pattern.
When to use the Template method pattern?
You want to use this pattern when faced with the following problems:
- You need to keep parts of an algorithm or a process the same but have some methods implement the specialized behavior: The basic steps of the algorithm are the...