
Episode 22 · Season 5
Software Design - The Template Method Pattern
About this episode
In the last episode, we looked at abstracting algorithms. Now we examine the template method pattern and how it allows us to abstract steps of an algorithm. Thus, providing a template for implementation while leaving the freedom for subclasses to handle the details.
The Template Method Pattern Defined
As always, we will start with the "Gang of Four" intent to set the stage for our discussion.
"Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure."
This is a rather simple pattern to understand. We build a root or base class that lays out the steps of an algorithm. Then we leave some (or all) of those steps to be implemented in subclasses. Note that we will usually implement some of the steps in the base class to avoid duplication of code. These steps performed in the base may also provide context and constraints for the subclasses to conform to.
Applying The Pattern
As noted above, most of the time this will start with a base class and some of the steps implemented. However, that is not a requirement. You could also start from an interface, but then it may make more sense to stay a little more flexible and use a strategy. In any case, the concrete part of the implementation will be wrapped up in the subclasses. This allows you to follow the same steps for the algorithm but do so in varying ways. A typical application of this pattern is a text processing algorithm that "cleans" data as it goes and has very different work to do in cleaning a date, a street address, or a phone number. However, this is a pattern that has such a broad number of uses you may easily see it turn up in almost any algorithm.
Java, PHP, C#, etc.
This pattern sticks to the core features of a language. All you need to implement this is a base class and inheritance. Therefore, the only differences among languages are going to be minor syntax variations to note what needs to be implemented or overridden in the child classes.
More from Develpreneur: Become a Better Developer and Entrepreneur
A handful of people love your MVP. Their suggestions keep coming, and AI helps you turn those requests into working features quickly. The product is growing. Whether it is growing in the right directi…
Your team can generate an interface faster than ever. Meanwhile, customers are still staring at confusing error messages, waiting for pages to load, and trying to figure out what happens after they cl…
After a year of deadlines, long hours, and projects that seemed determined to last forever, Thanksgiving offers a chance to pause and enjoy what all that effort has made possible. In this Thanksgiving…
The holiday is almost here, but your brain is still at work. There is something to finish, something to check, and probably something you could squeeze in before everyone arrives. Closing the laptop s…
Building a successful business requires more than developing a great product. As companies grow, they face new challenges involving customer expectations, operational efficiency, financial decisions,…