← Back to scenarios
ScenarioCatalog DB

Order processing template

Use one high-level order-processing sequence while varying validation and fulfillment details by order type.

Problem

An order system must preserve a consistent processing sequence while allowing order-type-specific validation and fulfillment steps to vary without duplicating the shared workflow.

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