liveBook · Manning

Template method design pattern

As explained in Object-Oriented Software Design in C++

The Template Method Design Pattern is a behavioral design pattern that defines an algorithm's fixed sequence while allowing subclasses to vary selected steps.

Use the Template Method Design Pattern when an algorithm must follow a fixed sequence, while selected steps vary between implementations. A sports-report generator illustrates the problem: baseball and volleyball reports share an overall structure but require different data acquisition, analysis, and presentation. section 8.0 section 8.1

Refactor the design around an abstract superclass and specialized subclasses:

  • Let the superclass define the algorithm outline and expose a template method that calls each step in the required order. section 8.1.3
  • Implement shared behavior, such as print_header() and print_footer(), once in the superclass. section 8.1.3
  • Declare varying steps for subclass implementation, such as acquire_data(), analyze_data(), and print_report(). section 8.1.3
  • Keep each concrete subclass focused on its own algorithm variations; BaseballReport and VolleyballReport implement their respective report details. section 8.1.3 section 8.4

Game reports

GameReport::generate_report() calls print_header(), acquire_data(), analyze_data(), print_report(), and print_footer() in order. BaseballReport supplies baseball statistics and a bar chart; VolleyballReport supplies the winner, score, and round-by-round scoring. The sequence remains stable while the report-specific steps change. section 8.1 section 8.1.3

Figure 8.3 This version of the application is modeled from the Template Method Design Pattern. The superclass GameReport outlines the steps in the proper order for the algorithm to generate a report. It implements the common steps print_header() and print_footer() and delegates the remaining steps acquire_data(), analyze_data(), and print_report() to the BaseballReport and VolleyballReport subclasses. The public template method generate_report() in the superclass calls the step member functions in the proper order. The grayed-out portions of the diagram haven’t changed logically from figures 8.1 and 8.2.

Use the class diagram to compare the fixed GameReport workflow, shared steps, and delegated subclass steps. section 8.1.3

How Object-Oriented Software Design in C++ covers it

How deep it goes
Defines the superclass, template method, common implementations, abstract steps, and concrete report subclasses.
Examples
Uses baseball and volleyball game reports, including statistics, bar charts, winning scores, and round-by-round scoring.
What it assumes
Assumes familiarity with algorithms and data, then teaches the pattern through concrete report-generation examples.
What you can do after
Build an inheritance-based algorithm skeleton that preserves execution order while allowing subclasses to implement selected steps.
How it connects
Connects inheritance with the Open-Closed Principle, Don't Repeat Yourself Principle, Encapsulate What Varies Principle, and Strategy Design Pattern.

Also explained in

Compare how 2 books explain template method design pattern →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Object-Oriented Software Design in C++. The section links point to the parts of the book it draws on.