> Design Patterns > Template Method Design Pattern

Template Method Design Pattern

Template Method design pattern is used to get some steps of algorithm implemented in subclass.

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 structure.

The intent of template method design pattern is to defer some steps of algorithm to subclass without changing the algorithm structure.

Problem

  • The algorithm has to be generic but some steps are require to be defined by user.

There may be a situation where steps of algorithm are not known. For example, the framework has some steps of algorithm which are supposed to be provided by user. There may be many scenarios where some steps may vary but the algorithm is generic.

Solution

  • Identify the invariant and variant part of the algorithm.
  • Have the implementation of invariant part in abstract class and variant part in derived class.

So the algorithm structure is not changed and some steps are deferred to be implemented by subclasses.

Where template method design pattern is applicable?

  • The algorithm is generic with invariant and variant parts.
  • The variant parts are required to be deferred to derived class.
  • There is a need to provide extension points.

Template Method Design Pattern UML Structure

Template Method Design Pattern UML Structure

Participant classes of template method design pattern

  • AbstractClass has method TemplateMethod() which defines a generic algorithm. It also has abstract methods PrimitiveOperation1() and PrimitiveOperation2(). These methods are variant parts of generic algorithm().
  • ConcreteClass implements the abstract methods PrimitiveOperation1() and PrimitiveOperation2().

How they work together?

  • Client uses the TemplateMethod(). The generic algorithm defined in TemplateMethod() calls the method of Concrete class to perform the deferred steps.
  • The invariant part of algorithm is executed in template method of abstract class and variant part of algorithm is executed in concrete class. This pattern is very useful for code reuse and generally used in class libraries and frameworks.

Template Method Design Pattern Example

Here is the template method design pattern example.

Network Management Framework want to configure devices - system, printer, network etc. It should be done in 3 steps - Initialize, Configure and Save. Configure will differ for each device.

Implementation Code

C++ Template Method Pattern in C++ Template Method Pattern C++ Example
C# Template Method Pattern in C# Template Method Pattern C# Example
Java Template Method Pattern in Java Template Method Pattern Java Example
Python Template Method Pattern in Python Template Method Pattern Python Example
JavaScript Template Method Pattern in JavaScript Template Method Pattern JavaScript Example
PHP Template Method Pattern in PHP Template Method Pattern PHP Example
Ruby Template Method Pattern in Ruby Template Method Pattern Ruby Example
Swift Template Method Pattern in Swift Template Method Pattern Swift Example
Objective-C Template Method Pattern in Objective-C Template Method Pattern Objective-C Example


Suresh Kumar Srivastava is founder of online learning site coursegalaxy.com and author of popular books C In Depth, Data Structures Through C In Depth. He has 18+ years experience in industry and worked on architecture and design of multiple products. This article Template Method Design Pattern is from his Design Patterns course.