12.6 The Template pattern
The Template pattern (sometimes called the Template method) is useful for removing duplicate code; it’s intended to support the Don’t Repeat Yourself design principle we discussed in Chapter 5. It is designed for situations where we have several different tasks to accomplish that have some, but not all, steps in common. The common steps are implemented in a base class, and the distinct steps for each subclass are overridden to provide custom behavior. This is built on the Strategy pattern; in a way, it’s an application of Strategy to a more specialized need. Here it is in the UML format:
This may seem like a straightforward application of inheritance. That’s because it is generally pretty clear what’s going on. The interesting spin is the idea that there’s a general template for a complicated process, and steps of the process...