Share This Article
The template method pattern in Ruby is a behavioral design pattern. It is used to define an algorithm in a base class but to leave some steps for subclasses to implement.
Why? Because some steps of an algorithm may be different for different subclasses. And this is exactly where the template method pattern shines.
The template method pattern is also known as the ‘Hollywood principle’ because the subclasses are like actors who know what to do, but they have to wait for the base class (the director) to tell them when to do it.
In this article, you will learn how to implement the template method pattern in Ruby.
The Problem
Let’s say you are building an online store and want to give your users the option to pay using PayPal or Credit Card.
The process for each payment method is different, but there are some steps that are common to both.
For example, you need to validate the user’s data, you need to calculate the total amount, and you need to send an email confirmation.
The Solution
You can use the template method pattern to define a base class with the common steps and to create subclasses for the different payment methods.
When a user chooses a payment method, you can instantiate the corresponding subclass and call the pay
method.
How to Implement the Template Method Pattern in Ruby
Let’s see a concrete example.
We are going to implement a simple ‘animal sound’ program.
Our program will have a base class (Animal) and two subclasses (Dog & Cat).
The base class
class Animal
def initialize
@sound = ''
end
def make_sound
puts @sound
end
end
The base class has an initialize
method to set the @sound instance variable and a make_sound
method to print the animal sound to the console.
There’s no point in calling the make_sound
method on the base class because the @sound instance variable is empty.
We will override this method in the subclasses to provide the animal sound.
The dog class
class Dog < Animal
def initialize
@sound = 'Woof'
end
end
The dog class overrides the initialize
method to set the @sound instance variable.
We don’t need to override the make_sound
method because it already does what we need.
The cat class
class Cat < Animal
def initialize
@sound = 'Meow'
end
end
The cat class overrides the initialize
method to set the @sound instance variable.
We don’t need to override the make_sound
method like the dog.
The client code
Now that we have our base class and subclasses, let’s see how to use them in our client code.
dog = Dog.new
dog.make_sound # => Woof
cat = Cat.new
cat.make_sound # => Meow
We are instantiating the Dog class and calling the make_sound
method.
This will print ‘Woof’ to the console.
We are doing the same with the Cat class, which will print ‘Meow’ to the console.
The Template Method Pattern in Rails
The template method pattern is widely used in Ruby on Rails.
For example, Rails uses the template method pattern to render views.
When you use the render
method in your controller, Rails will look for a view with the same name as the controller action.
If the view exists, Rails will render the view.
In case the view doesn’t exist, Rails will look for a layout with the same name as the controller.
If the layout exists, Rails will render the layout.
In case the layout doesn’t exist, Rails will look for a partial with the same name as the controller (in the app/views/application
directory).
If the partial exists, Rails will render the partial.
If none of the above exists, Rails will render an error.
As you can see, the render
method uses a template to render the view, the layout, or the partial.
Summary
In this article, you’ve learned the template method pattern and how to implement it in Ruby.
You’ve also seen how to use the template method pattern to render a view in Ruby on Rails.
Here’s what you should remember from this article:
- The template method pattern is a behavioral design pattern used to define an algorithm in a base class but leaves some steps for subclasses to implement.
- This pattern is also known as the ‘Hollywood principle’ because the subclasses are like actors who know what to do, but they have to wait for the base class (the director) to tell them when to do it.
- The template method pattern is widely used in Ruby on Rails.