W. Kandinsky, Improvisation VI(African), 1909
This site has been moved to http://www.gol27.com/
Design Patterns : Elements of Reusable Object - Oriented Software
by Erich Gamma, Richard Helm, Ralh Johnson, John Vlissides
QA76.64.D47 1994
-
Template Method
Applicability
- Use the Template Method when
- to implement the invariant parts of an algorithm once
and leave it up to subclasses to implement the behavior that can vary.
- when common behavior among subclasses should be factored
and localized in a common class to avoid code duplication.
You first identify the differences in existing code and
then separate the differences into new operations.
Finally, you replace the differing code with a template method
that calis one of these new operations.
- to control subclases extensions. You can define a template method
that calls "hook" oyerations at specific points,
thereby permitting extensions only at those points.
Structure
Participants
-
- AbstractClass
- defines abstract primitive operations
that concrete subclasses define to implement steps os an algorithm.
- Implements a template method defining the skeleton of an algorithm.
The template method calls primitive operations
as well as operations defined in AbstractClass or those of other objects.
- ConcreteClass
- implements the primitive operations to carry out subclass-specific steps of algorithm.
ConcreteClass relies on AbsractClass to implement the invariant steps of the algorithm.