The Template Method in Ruby

The Template Method in Ruby
Average rating: 1
(1 votes)

Thanks! You’ve rated this material!

Syndicode’s blog is the source where you can find a lot of useful information on almost every topic a modern developer and a business owner needs to know. We write about business, updates, strategies, technologies, design, marketing, and programming. And as Ruby on Rails development agency, today we want to share with you some information about our language of choice. Read about the Template Method in Ruby.

The Template Method is a powerful tool for keeping your code solid and dry. We have discovered a good article Design Patterns & Ruby: The Template Method Pattern by Josh Saint Jacque where you can find some code examples.

In our material we prepared for you several important extras from it.

Most of us do underestimate the design patterns in Ruby. Ruby’s expressiveness and readability make it easy to forget that the maintainability of code largely depends on its design. Solid design should be considered for creating a better code. Of course, design patterns are not always the answer, but they certainly can help to write more testable, readable code. By knowing and using one of the simpler patterns, – the Template Method, – you’ll be able to approach the design of your code with much more confidence.

The Template Method allows you define the “skeleton” of an algorithm while leaving concrete implementation up to inheriting classes. The template serves as an outline of the steps needed to complete the task. It’s concrete-class children then go on to implement those missing steps.

The Template Method scheme by Josh Saint Jacque

From the scheme, you can see that child classes get their parent’s methods for free, but can also override them at will.

If you have one or more steps need to vary, this pattern will be very useful. You can cleanly separate concerns into their own implementations with no need in multiple classes.  But don’t use this pattern just because you think part of an algorithm might be changed one day. Just start with a concrete class and, if a new requirement ever actually arrives, only then consider breaking out the implementation into abstract and concrete classes.

Moreover, the Template Method helps us to separate code that changes from code that doesn’t. It helps us keep our concerns cleanly separated, and makes future change easier and safer. On the other hand, the inheritance on which this pattern relies can be a sledgehammer. You have to wisely use this pattern, especially in cases where the composition is available instead.

Imagine that over the time your code for generating a financial report (let’s take this example) should be changed. By designing this code with the Template Method pattern it will be easier and safer to change it in the future. You can’t break non-dependent code that you don’t have to change. In the original article you will find the example of code for this case.

We reminded you about this method and hope you will use it in your practice to get better and safer code. It’s great when you have an algorithm that needs to change, but only in part, in different contexts. As with all things, it can be abused, so be diligent in its application.

Subscribe to our weekly newsletter to get all the important news directly into your mailbox!

Rate this article, if you like it

Thanks! You’ve rated this material!

Got a project? Let's discuss it!

    Kyiv Sofiivska 1/2a, 01001, Kyiv, Ukraine
    Dnipro Hlinky 2, of. 1003, 49000, Dnipro, Ukraine
    Kharkiv Otakara Yarosha 22, 61000, Kharkiv, Ukraine