Template Method
TypeScriptApplicationTypeScript 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}`);
}
}