7
← Back to Pattern Arsenal
behavioral

Template Method

good to-know

Stage 1: Problem

The Problem

You are building a report generation system for an analytics dashboard. The system must produce reports in three formats: PDF, CSV, and Excel.

Every report follows the same workflow:

  1. Fetch raw data from the database
  2. Validate that the data meets minimum thresholds
  3. Transform/aggregate the data
  4. Render the final output in the target format

The first three steps are mostly identical across formats. Only the final rendering step differs significantly, though some formats need slight tweaks in transformation too (CSV needs flat rows, Excel needs grouped sheets).

Your PM says: "We'll add HTML and Markdown formats next quarter."

Before reading further: How would you structure this so that adding a new format is easy, the shared logic lives in one place, and each format can customize the steps it needs to?


Related Patterns

Confused With a Similar Pattern?

1 / 10 — Problem