← Back to scenarios
ScenarioCatalog DB

Shipping cost calculation

Choose a shipping-cost algorithm by delivery type without one large conditional block.

Problem

An order service must calculate shipping with interchangeable delivery-pricing algorithms without centralizing all delivery-type rules in a growing conditional structure.

Related patterns

Implementations

Strategy

TypeScriptApplication

TypeScript payment method strategies

Encapsulates card and PayPal payment behavior behind a shared interface so checkout selects a payment algorithm without branching through each method.

TypeScript
interface PaymentStrategy {
  pay(amountInCents: number): Promise<void>;
}

class CardPayment implements PaymentStrategy {
  async pay(amountInCents: number) {
    console.log(`Charging card for ${amountInCents} cents`);
  }
}

class PayPalPayment implements PaymentStrategy {
  async pay(amountInCents: number) {
    console.log(`Charging PayPal for ${amountInCents} cents`);
  }
}

class CheckoutService {
  constructor(private readonly paymentStrategy: PaymentStrategy) {}

  checkout(amountInCents: number) {
    return this.paymentStrategy.pay(amountInCents);
  }
}

Associated technologies