Template Method
Intent
Define the skeleton of an algorithm in an operation, deferring some
steps to subclasses. Template Method lets subclasses redefine certain
steps of an algorithm without changing the algorithm's structure.
Participants
-
Abstract class -
defines the outline of a template method.
-
Concrete class -
subclasses the abstract class and refines the steps in a template
method.
Other Terms
-
Template method -
a method that is implemented in the abstract class as an outline with
steps that can be defined or redefined by concrete classes.
-
Primitive operation -
a step operation that is declared abstract in the abstract class.
Concrete classes must provide an implementation.
-
Hook operation -
a step operation that is given a default implementation in the
abstract class.
Concrete classes may override this implentation.
-
Helper method -
a method, implemented in the abstract class, that can be used by
concrete classes in their implementation of primitive or hook
operations.
Notes
Many primitive and hook operations and helper methods are only invoked by
concrete classes.
Such operations and methods can be declared protected
.
Example