Template pattern
The template pattern is useful for removing duplicate code; it's an implementation to support the "Don't Repeat Yourself" 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 different steps are overridden in subclasses to provide custom behavior. In some ways, it's like a generalized strategy pattern, except similar sections of the algorithms are shared using a base class. Here it is in UML format:
Template example
Let's create a car sales reporter as an example. We can store records of sales in an SQLite database table. SQLite is a simple file-based database engine that allows us to store records using SQL syntax. Python 3 includes SQLite in its standard library, so there is no setup involved.
We have two common tasks we need to perform:
Select all sales of new vehicles and output them to the screen in a comma...