Template Method

From CSSEMediaWiki
Revision as of 03:22, 25 November 2010 by WikiSysop (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Intent

To allow the skeleton of an algorithm to be defined in a superclass, so that subclasses can redefine certain steps (methods/operations) of the algorithm as they see fit without changing the structure of the algorithm. The algorithm (template method) is defined in the superclass as abstract methods/operations in a fixed order.

When to use it

The template method should be used:

  • To implement the parts of an algorithm that are fixed, and let the subclasses implement the behaviour in their own way.
  • To refactor common subclass behaviour into the superclass to avoid code duplication.
  • To define particular extension points for subclasses. You can define an algorithm and call certain operations at points in the algorithm that can be implemented by subclasses. In this way, you restrict the extension of the algorithm to the implementation of these hook operations.

Structure

TemplateMethod.png

Participants

AbstractClass

This class defines the abstract primitive operations that the subclasses override to implement particular steps of the algorithm. It also implements the template method which is essentially the skeleton of the algorithm. The template method will call the primitive operations for the parts of the algorithm it doesn't want to implement itself.

ConcreteClass

This class implements the primitive operations to perform certain steps of the algorithm.

Example

An example of a template method is the fullMoon() method of the Lycan class in our Monsters design. Here is the relevant part of the diagram:

Template example.png

The fullMoon() method in the Lycan class is the template method. As you can see the fullMoon() method is an algorithm that calls the howl(), hunt(), bite() and howl() methods in a specific order - these are the operations. The subclasses Wererabbit and Werewolf redefine the howl(), hunt() and bite() operations to do the specific actions for their type of class.


Important: note that the template method relies on the fundamental object oriented idea of Polymorphism. That is, even if we send the fullMoon() message to a Lycan variable which is actually a Werewolf object, the Werewolf's fullMoon() method will be the one that is actually called, and hence the operation methods of the Werewolf class will be used.

Another Example

You can find here a nicely explained java code example of the Template Method usage in the real world.

Recognising the pattern

Classes: AbstractClass, multiple ConcreteClasses

  • templateMethod() that defines a skeleton of abstract protected method(s) in AbstractClass.
  • AbstractClass has at least one subclass ConcreteClass.
  • ConcreteClasses must implement all protected methods.

Consequences

  • The Template Method allows common behavior to be factored out into the superclass while allowing the steps that may vary to be moved to the subclasses.

Related Patterns

  • Factory Method: Template methods often call Factory Methods to create objects.
  • Strategy: The template method uses inheritance to vary the algorithm while Strategy uses composition and delegation.

See also


Personal tools