← Back to scenarios
ScenarioCatalog DB

Report generation template

Use one report-generation flow while different report types customize data collection and formatting.

Problem

A reporting system must keep its common generation sequence stable while allowing report types to customize selected steps such as data collection and formatting.

Related patterns

Implementations

Template Method

TypeScriptApplication

TypeScript order processing template

Keeps the order-processing sequence fixed while subclasses customize validation and fulfillment for different order types.

TypeScript
abstract class OrderProcessor {
  async process(orderId: string) {
    await this.validate(orderId);
    await this.reserveInventory(orderId);
    await this.fulfill(orderId);
  }

  protected abstract validate(orderId: string): Promise<void>;

  protected async reserveInventory(orderId: string) {
    console.log(`Reserving inventory for ${orderId}`);
  }

  protected abstract fulfill(orderId: string): Promise<void>;
}

class DigitalOrderProcessor extends OrderProcessor {
  protected async validate(orderId: string) {
    console.log(`Validating digital order ${orderId}`);
  }

  protected async fulfill(orderId: string) {
    console.log(`Emailing download link for ${orderId}`);
  }
}

Associated technologies