← Back to scenarios
ScenarioCatalog DB

Home theater startup

Simplify a multi-device home-theater startup sequence into one watch-movie operation.

Problem

A user must start a movie without manually coordinating the display, audio system, media player, and other home-theater devices in the correct order.

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