The Template Method Pattern

archetype PublishingObject-Oriented Design

Categories: software-engineering

What It Brings

A template is a document with blanks — a form letter with spaces for the name and date, a stencil with cutouts for paint, a page layout with placeholders for content. The GoF Template Method pattern maps this onto software: a base class defines the skeleton of an algorithm with certain steps left as abstract methods that subclasses fill in. The publishing metaphor frames inheritance as a printing process: the template is fixed, and each edition supplies its own content for the variable slots.

Key structural parallels:

Where It Breaks

Expressions

Origin Story

The concept of algorithmic templates predates the GoF book. Frameworks in the 1980s — particularly application frameworks in Smalltalk and early C++ — relied heavily on base classes that defined processing skeletons with overridable steps. The GoF formalized this as the Template Method pattern in 1994, choosing a name that borrows from publishing rather than manufacturing or architecture. “Template” emphasizes the fixed structure with variable content, which is arguably the clearest metaphor in the GoF catalog for its intended concept. The name has become somewhat ironic in modern development: C++ templates, template engines (Mustache, Jinja), and HTML templates all use the word “template” for different concepts, creating terminological confusion that the original publishing metaphor didn’t anticipate.

References

Related Mappings