← Back to scenarios
ScenarioCatalog DB

Report generation

Hide data collection, formatting, rendering, and delivery behind one report-generation operation.

Problem

A client must generate and deliver a report without coordinating the underlying data collection, formatting, rendering, and delivery components.

Related patterns

Implementations

Facade

TypeScriptApplication

Facade in TypeScript

Provides one checkout operation that coordinates inventory, payment, and shipment subsystems without exposing their individual workflows to callers.

TypeScript
type OrderRequest = {
  orderId: string;
  sku: string;
  quantity: number;
  paymentToken: string;
};

class InventoryService {
  reserve(sku: string, quantity: number) {
    console.log(`Reserved ${quantity} of ${sku}`);
  }
}

class PaymentService {
  charge(paymentToken: string) {
    console.log(`Charged payment token ${paymentToken}`);
  }
}

class ShipmentService {
  createShipment(orderId: string) {
    console.log(`Created shipment for ${orderId}`);
  }
}

class CheckoutFacade {
  constructor(
    private readonly inventory: InventoryService,
    private readonly payments: PaymentService,
    private readonly shipments: ShipmentService,
  ) {}

  placeOrder(request: OrderRequest) {
    this.inventory.reserve(request.sku, request.quantity);
    this.payments.charge(request.paymentToken);
    this.shipments.createShipment(request.orderId);
  }
}

const checkout = new CheckoutFacade(
  new InventoryService(),
  new PaymentService(),
  new ShipmentService(),
);

checkout.placeOrder({
  orderId: "order-123",
  sku: "keyboard",
  quantity: 1,
  paymentToken: "token_abc",
});

Associated technologies