Adapter
Wrap an existing class whose interface doesn't fit, so it works with code that expects a different interface.
Description
An adapter sits between your code and something you can't or don't want to change: a vendor SDK, a legacy module, an old API version. It implements the interface your code expects and translates each call into the shape the other side understands.
In the example the app speaks charge(card, Money), while the payment SDK wants cents and numeric currency codes and returns its own status strings. The adapter converts in both directions, and the rest of the app never sees the SDK's quirks.
Class diagram
Adapter pattern class diagram
- Class Checkout with + checkout(gateway, card, price).
- Interface PaymentGateway with + charge(card, price): Receipt.
- Class LegacyPayAdapter with + charge(card, price): Receipt.
- Class LegacyPayClient with + makePayment(cents, code, token).
- Checkout uses PaymentGateway.
- LegacyPayAdapter implements PaymentGateway.
- LegacyPayAdapter translates to LegacyPayClient.
Sequence diagram
Adapter pattern sequence diagram
- Checkout → LegacyPayAdapter: charge("tok_4242", 12.50 EUR).
- LegacyPayAdapter → LegacyPayAdapter: 12.50 EUR → 1250, 978.
- LegacyPayAdapter → LegacyPayClient: makePayment(1250, 978, token).
- LegacyPayAdapter receives "{ status: "OK", txn }" back from LegacyPayClient.
- Checkout receives "{ ok: true, reference }" back from LegacyPayAdapter.
TypeScript example
Checkout code written against a clean PaymentGateway interface, and an adapter that makes a legacy SDK fit it: cents, ISO currency codes and status strings are translated in one place.
// Adapter: make a legacy payment SDK fit the interface the app expects.
interface Money {
amount: number; // in major units, e.g. 12.5
currency: "EUR" | "SEK" | "USD";
}
interface Receipt {
ok: boolean;
reference: string;
}
// Target: the interface our checkout code is written against
interface PaymentGateway {
charge(card: string, price: Money): Receipt;
}
// Adaptee: a vendor SDK with a different shape (we can't change it)
class LegacyPayClient {
makePayment(cents: number, currencyCode: number, cardToken: string): { status: "OK" | "DECLINED"; txn: string } {
const declined = cardToken.endsWith("0000");
return { status: declined ? "DECLINED" : "OK", txn: `LP-${currencyCode}-${cents}` };
}
}
// Adapter: implements the target, translates every call to the adaptee
class LegacyPayAdapter implements PaymentGateway {
private static readonly currencyCodes = { EUR: 978, SEK: 752, USD: 840 };
private readonly client: LegacyPayClient;
constructor(client: LegacyPayClient) {
this.client = client;
}
charge(card: string, price: Money): Receipt {
const cents = Math.round(price.amount * 100);
const result = this.client.makePayment(cents, LegacyPayAdapter.currencyCodes[price.currency], card);
return { ok: result.status === "OK", reference: result.txn };
}
}
// Client code: knows only PaymentGateway
function checkout(gateway: PaymentGateway, card: string, price: Money): string {
const receipt = gateway.charge(card, price);
return receipt.ok ? `paid, ref ${receipt.reference}` : `declined (${receipt.reference})`;
}
export function demo(): string[] {
const gateway: PaymentGateway = new LegacyPayAdapter(new LegacyPayClient());
return [checkout(gateway, "tok_4242", { amount: 12.5, currency: "EUR" }), checkout(gateway, "tok_0000", { amount: 99, currency: "SEK" })];
}When to use it
Use it when
- You integrate a third-party library or service whose interface doesn't match yours.
- You're replacing one vendor with another and want both behind the same interface during the switch.
- Legacy code has to keep working while new code is written against a cleaner interface.
- You want vendor types and error codes to stop at one boundary instead of leaking through the codebase.
Avoid it when
- You own both sides and can simply change one interface to match the other.
- The interfaces already match. A pass-through wrapper adds a layer and nothing else.
- The mismatch is in behaviour, not shape, such as different consistency or ordering guarantees. A thin adapter can hide that, which is dangerous.
Pitfalls
- Leaky adapters that still return vendor types or throw vendor errors. Translate errors and results too, not just arguments.
- Adapters that slowly gain business logic. Keep them to translation; put rules elsewhere.
- Lossy conversions, such as rounding money or dropping fields, that nobody notices. Test the translation explicitly.
Alternatives
Facade
Choose it when you want one simple interface over a whole subsystem, not a translation of one interface into another.
Decorator
Choose it when the interface stays the same and you want to add behaviour around it.
Proxy
Choose it when the interface stays the same and you want to control access to the real object.
Anti-Corruption Layer
Choose it when you're translating between whole systems or bounded contexts, not one class.