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

