Chapter 9. The Template Method Pattern
It is easy to study the rules of overloading and of templates without noticing that together they are one of the keys to elegant and efficient type-safe containers.
All the great legends are Templates for human behavior. I would define a myth as a story that has survived.
I work out the other bits, too, but I need to know what I look like, very early on. And then it’s like a template; I’ll fill that person out. If I get that out of the way, then I’m all right.
What Is the Template Method Pattern?
First of all, you need to differentiate between the
Template Method as a design pattern and the templateMethod()
—a class method—used within a
Template Method pattern. The templateMethod()
is a concrete method within an
abstract class. It orders the sequence of abstract methods, leaving the
implementations up to a concrete class. At its core, the Template Method
defines the skeleton of an algorithm in an operation while the exact
implementations are left up to the concrete classes.
The good thing about the Template Method is that it is relatively small and easy to implement. A single abstract and concrete class is all you need, as shown in Figure 9-1.
Figure 9-1. Template Method class diagram
One way to think of the template method operation in the abstract class is as an arranger of primitive operations. (I will ...
Get Learning PHP Design Patterns now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.