Adapter

Intent: Convert the interface of a class into another interface clients expect.

When to use it

  • Integrating a third-party API with a different shape
  • Reusing legacy code

Example

One PaymentGateway interface wrapping Razorpay, Stripe and PayPal.

Code sketch (TypeScript)

interface Gateway { charge(amount: number): Promise<string> }
class RazorpayAdapter implements Gateway { charge(a) { return razorpay.orders.create({ amount: a }); } }

Learn patterns step by step in the Learn area →