Skip to content

Latest commit

 

History

History

template-method

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 

Intend

As stated in GoF, p325 :

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.

It looks like the "inheritence" version of the "composition" pattern Strategy pattern

Template Method works at the class level, so it is static when Strategy works on the object level, letting you switch behaviors at runtime.

You should use the Template method pattern in the following cases :

  • You want clients to be able to extend certain parts of an algorithm, but not the complete algorithm.
  • You want to implement the skeleton of an algorithm once and leave the responsability of implementation to subclasses.
  • You have multiple classes with common behavior and you want to factor the common parts.

How it's done

UML

Participants

  • Abstract class
    • Defines the primitive operations that need to be implemented by the ConcreteClasses in order to implement the algorithm.
    • Implements a template-method defining the skeleton of the algorithm.
  • ConcreteClass
    • Implements the primitive operations of the Abstract class to define the algorithm.

How to implement

  1. Break your algorithm into two parts : common steps VS unique steps.
  2. Create an interface to put the steps of the algorithm and only leave the unique steps open to subclassing.
  3. Add hooks between crucial steps of the algorithm.
  4. Create as many subclasses as particular algorithms and implement their unique steps.

Note : UML class diagram taken from here

Pros & cons

Pros

  • Clarity/readability : Remove duplicate code and factor it into a superclass.
  • Control : You can let clients override only certain parts of a large algorithm.

Cons

  • The skeleton provided by the Template method pattern might limit clients.
  • Might become difficult to maintain if there are many steps.

Notes

Here are some usefull ressources :