Connect With Us

Template Method Design Pattern in Rails

By Neha Suwal on Nov, 30, 2016 in template-method-design-pattern, design-pattern, ruby, programming

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.
Example:
class Animal
  attr_reader :type
  def initialize(type)
    @type = type
  end
  
  def speak
    if @type == :cow
      puts "MOOO MOOO"
    elsif @type == :pig
      puts "OINK OINK"
    elsif @type == :duck
      puts "QUACK QUACK"
    end
  end
end


Animal.new(:cow).speak
Animal.new(:pig).speak
Animal.new(:duck).speak
Output:

MOOO MOOO
OINK OINK
QUACK QUACK

Using template method:

class Animal
  def speak
    raise NotImplementedException
  end
end


class Cow < Animal
  def speak
    puts "MOOO MOOO"
  end
end


Cow.new.speak
Animal.new.speak

Output:

MOOO MOOO
uninitialized constant Animal::NotImplementedException (NameError)

Advantage:

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

Disadvantage:

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

Slideshare: http://bit.ly/2gs4qgH

Reference:

 

Share This:

Comments


Add a new comment

Latest Posts


First Step to Software Development: Design Thinking Process By Rasna Shakya on Nov, 03, 2017

Bourbon By Rasna Shakya on Oct, 20, 2017

Design Sprint By Neha Suwal on Aug, 14, 2017

3 years of helping startups and entrepreneurs with technology By Neha Suwal on Aug, 04, 2017

Digital Marketing for Startups By Neha Suwal on Jun, 30, 2017