The Template Pattern

Learn about the Template pattern with the help of an example in Python.

We'll cover the following


The Template pattern (sometimes called the Template method) is useful for removing duplicate code; it’s intended to support the Don’t Repeat Yourself (DRY) principle. 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 are overridden in subclasses to provide custom behavior. In some ways, it’s like the Strategy pattern, except similar sections of the algorithms are shared using a base class. Here it is in the UML format:

Get hands-on with 1200+ tech skills courses.