Skip to main content

Template Method Pattern

Structural: Template Method

Can you explain the template method design pattern?

View Answer:
Interview Response: The Template Method pattern defines a set of stages in an algorithm. Objects that implement these steps keep the algorithm's original structure but have the option to redefine or alter specific steps. This pattern intends to provide the client developer with extensibility.

Code Example:

This pattern's objects are as follows:

AbstractClass -- example code: datastore

  • It provides a way for clients to use the template method.
  • It uses the template method to define the basic steps of an algorithm.
  • It provides hooks (through method overriding) for a client developer to use in implementing the Steps.

ConcreteClass -- example code: MySQL

  • carries out the primitive Steps described in AbstractClass.

let datastore = {
process: function () {
return true;

function inherit(proto) {
let F = function () {};
F.prototype = proto;
return new F();

function run() {
let mySql = inherit(datastore);

// implement template steps

mySql.connect = function () {
console.log('MySQL: connect step');
}; = function () {
console.log('MySQL: select step');

mySql.disconnect = function () {
console.log('MySQL: disconnect step');





MySQL: connect step
MySQL: select step
MySQL: disconnect step


What pattern category does the template method pattern belong to?

View Answer:
Interview Response: The Template Method pattern is a behavioral design pattern.

What is the use case for the template method design pattern?

View Answer:
Interview Response: When to Use the Template Method Pattern.

  • The template method pattern solves the problem by employing an algorithm with various versions. You need to divide your method into additional steps implemented in the abstract class when the different implementations share them. On the other hand, we implement the various steps in the concrete classes.
  • Another compelling use case for this approach is when you have copied and pasted code (private functions) between various classes.
  • Finally, you can employ this strategy if most of your classes exhibit similar tendencies.

What are some of the benefits of using the Template Method pattern?

View Answer:
Interview Response: Benefits of the Template Method Pattern

  • It's relatively easy to create a concrete implementation of an algorithm because you're removing common parts of the problem domain using an abstract class.
  • Clean code because you avoid duplicate code.
  • Ever cleaner code because you separate the algorithm into private methods or functions that are simpler and easier to test.

What are some disadvantages of employing the Template Method design pattern?

View Answer:
Interview Response: Drawbacks of the Template Method Pattern.

  • You may violate the Liskov Substitution Principle by suppressing a default step implementation through a subclass.
  • Some clients may be the only reason the template pattern imposes a specific design.
  • The template design is more adaptable than other patterns, and modifications at the high or low level might disrupt implementation, making maintenance difficult.