CoursesGalaxy Courses Subscription Knowledge Hub
Knowledge Hub
Design Patterns
    Creational Patterns
    Structural Patterns
    Behavioral Patterns
        Chain of Responsibility
        Command
        Interpreter
        Iterator
        Mediator
        Memento
        Observer
        State
        Strategy
        Template Method
        Visitor


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 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 it 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.
Structure

Template Method Design Pattern

Participant classes
  • 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.
Example

Implementation Code

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



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 is from his courses on design patterns.







Yearly Subscription


Design Patterns in Python


Advanced Data Structures in Java

Explore

Courses

Subscription

Knowledge Hub

Design Patterns

Tech Articles

About Us

Overview

Contact Us

CourseGalaxy

CourseGalaxy is focused on transformation of Engineering graduate to Software Engineer to fill the gap between education and industry and provide the training on specific problems of industry to enhance the skills of Engineers in industry to do their job appropriately.

Learn From Us

2018 CourseGalaxy