Template Method Pattern
Last updated
Last updated
Define the skeleton of an algorithm, but deferring some steps to subclasses without changing the algorithm's structure.
Used to implement the invariant parts of an algorithm once, and leave it upto subclasses to implement what varies.
When a common behavior among subclasses should be factored in a common clase to avoid duplication (DRY Principle).
When you want to let clients extend only particular steps of an algorithm, but not the whole algorithm or its structure.
When you need to let the client be able to extend the logic exactly at certain points and no more (using hooks methods).
AbstractClass: defines the primitive operations that concrete subclasses define to implement steps of an algorithm.
ConcreteClass: implements the primitive operations to carry out subclass-specific steps of the algorithm.
Usually AbstractClass will have abstract steps methods that should be implemented by the ConcreteClass, but there may also be Hooks Methods that are optional points of extension of the algorithm.
You may close the overriding of some functions if this is available on the language of implementation.
Template Method is a very good example for Open / Closed Principle.
Source Code
UML
// TODO
// TODO
You can find the tests here.