Connect With Us

DesignRush Go to Design Rush

Template Method Design Pattern in Rails

By Neha Suwal on Nov, 29, 2016

The Template Method pattern is a behavioral design pattern that defines the program skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure.


According to Gang of Four,
“Define a skeleton of an algorithm in an operation, deferring some steps to subclasses. It lets subclasses redefine certain steps of an algorithm without changing its subclasses.”

  •  `template_method` calls `step_one` method, `step_two` method and `step_three` method
  • `step_two` is a hook method (through method overriding). i.e It is declared in base class and then defined in subclasses

Where to use?

  • A class has more than one method that each contains an if/switch/case statement with 3 or more possibilities.
  • A class which has an if/switch/case statement that can have new possibilities as conditions in the future.
class Animal
  attr_reader :type
  def initialize(type)
    @type = type
  def speak
    if @type == :cow
      puts "MOOO MOOO"
    elsif @type == :pig
      puts "OINK OINK"
    elsif @type == :duck
      puts "QUACK QUACK"


Using template method:

class Animal
  def speak
    raise NotImplementedException

class Cow < Animal
  def speak
    puts "MOOO MOOO"


uninitialized constant Animal::NotImplementedException (NameError)


  • It avoids code duplication by reusing them. The shared code is available in the abstract class. So, you get to add only the part of the code that differs in the sub classes.
  • The subclasses can decide whether to reuse the parent class code, or to apply a different one. You get to override the functions from the parent class.


  • Because Template Design Pattern uses inheritance. It can be problematic. When debugging it becomes difficult to understand the sequence of the flow, because any method can be an override method or can be called from the parent implementation. You might end up implementing method from the abstract classes which you didn’t intended to do.

Design Patterns Similar to Template Method Pattern

Factory Pattern:

Factory Pattern also uses Inheritance. So, there is a chance you might confuse Template Pattern with it. Unlike Template Design pattern you leave the decision on which Subclass to use to the factory class. Think of it as Factory Method is to creating objects as Template Method is to implementing an algorithm.You can refer to the following link [Factory Design Pattern in Ruby]. I am sure. This will help you take better decisions on situations when you get confused which of these patterns to implement.

Strategy Pattern:

Strategy Pattern and Template Method Pattern serve very similar needs. They both allow the user to change the algorithm at run time. The basic difference is on how the implementations is carried out. It is inheritance versus aggregation (is-a versus has-a). With Strategy pattern the algorithm is encapsulated inside the implementing sub class.

You use Strategy Pattern when you need to change the algorithm of a specific method. Whereas, Template Method Pattern is used when you want to some changes( not all ) in the behaviours of the class.




Share This:

Latest Posts

Integrating Afterpay Payment in a Rails Project By Umesh P Kafle on Sep, 07, 2018

Switching to React Native from Ruby on Rails By Kamal Pandey on Aug, 29, 2018

Jyaasa in IT Career Fair 2018 By Sonika Thapa on Jun, 12, 2018

Encrypted secrets in Ruby on Rails By Rasna Shakya on Apr, 20, 2018

Rails Payment Integration with zipPay (Buy Now and Pay Later) By Nelson Suwal on Mar, 23, 2018