Origin: |
GoF 95 |
Reason: |
To allow subclasses of a common class to vary
portions or steps of a general algorithm.
|
Synopsis: |
A general algorithm implemented by a base class
calls abstract methods for some steps.
Subclasses of the base class specialize these methods.
|
Example: |
Consider the process of logging in and authenticating a user
for an application.
Some steps of the process, e.g., prompting the user are application
independent.
Other steps, e.g., authenticating the user, are
application dependent.
|
Solution: |
AbstractTemplate |
This class defines a method, the templateMethod,
that implements the general logic of an algorithm.
The templateMethod calls other methods that
are overriden by ConcreteTemplate.
|
ConcreteTemplate |
This class overrides methods that are called by
the templateMethod.
|
|
See also: |
Strategy
(modifies logic for individual objects rather than classes)
Factory Method
(specializes the construction of an object rather
than the execution of an algorithm)
|