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 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
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
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 methods from the abstract classes which you didn’t intend to do.
Design Patterns Similar to Template Method Pattern
Factory Pattern also uses Inheritance. So, there is a chance you might confuse the Template Pattern with it. Unlike the Template Design pattern you leave the decision on which Subclass to use to the factory class. Think of it as Factory Method is to create objects as Template Method is to implement an algorithm. You can refer to the following link Factory Design Pattern in Ruby. I am sure. This will help you make better decisions in situations when you get confused which of these patterns to implement.
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 implementation is carried out. It is inheritance versus aggregation (is-a versus has-a). With the Strategy pattern the algorithm is encapsulated inside the implementing subclass.
You use Strategy Pattern when you need to change the algorithm of a specific method. Whereas, Template Method Pattern is used when you want some changes( not all ) in the behaviors of the class.
- Command Design Pattern in Ruby
- How To Implement Law Of Demeter In Ruby
- Application Template in Rails
Gurzu is a full-cycle Ruby on Rails development company. Since 2014, we have built software running on the Ruby on Rails framework for startups and enterprises from all around the world using Agile methodology. Our team of experienced Ruby developers can help to develop your next product.
Have a tech idea you want to turn into reality? Book a free consulting call.