Template Method Pattern
- 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
Solution
two types of steps:
- abstract steps must be implemented by every subclass
- optional steps already have some default implementation, but still can be overridden if needed
Code Structure
/behavioral-design-patterns/template-method-pattern/template-structure.png)
Comparisons
Click here to expand...
Link to originalFactory Method Pattern vs Template Method Pattern
- Factory Method is a specialization of Template Method used specifically for creating objects rather than containing business logic
- At the same time, a Factory Method may serve as a step in a large Template Method
Link to originalStep Builder Pattern vs Template Method Pattern
- Step Builder is a specialized Template Method for object creation (with Builder Pattern)
Link to originalStrategy Pattern vs Template Method Pattern
- Template Method Pattern is based on inheritance: it lets you alter parts of an algorithm by extending those parts in subclasses
- Strategy Pattern is based on composition: you can alter parts of the object’s behavior by supplying it with different strategies that correspond to that behavior
- Template Method Pattern works at the class level, so it’s static after compile-time
- Strategy Pattern works on the object level, letting you switch behaviors at runtime