Intent - Define the skeleton of an algorithm for
an operation, deferring some steps to subclasses. Allows
redefinition of the algorithm steps without changing the
structure
Problem -
Common behavior among subclasses should be factored
and localized to a common class to avoid code duplication.
The steps of the algorithm are the same, but
implementation details are different in subclasses.
Solution - Allows for definition of substeps that
can vary in a specific subclass while maintaining the same
basic steps in the base class.
Participants and Collaborators
AbstractClass
ConcreteClass
Consequences
A good platform for code reuse
Can ensure code gets implemented
Template methods should distinguish between
operations that may be overridden and must
be overridden.
Because of above, default operations can be provided
by the base class.
Optionally, the template method may be declared final to
prohibit subclasses from changing the algorithm
Sometimes, the AbstractClass provides default implementation
of the suboperations that the subclass can override. These
are known as "hook" methods.