Skip to the content.

Adapter

The problem

The shop’s checkout is written against a clean CardPayments interface: charge(customer, amount) with BigDecimal pounds and a meaningful result. The payment provider it must actually use is a legacy gateway taking "CUST/<id>" reference strings and amounts in pence, returning magic integer status codes. Neither side can change: the gateway is third-party, and rewriting checkout around its quirks would spread STATUS_* constants through the whole codebase.

The pattern

An adapter implements the interface the client wants and translates every call onto the interface the legacy class provides:

CardPayments payments = new LegacyGatewayAdapter(new LegacyPaymentGateway());
PaymentResult result = payments.charge(alice, new BigDecimal("19.99")); // pounds in, meaning out

Benefits

Seen in the wild

InputStreamReader (adapts InputStream -> Reader), Arrays.asList() (array -> List), Collections.enumeration(), Spring MVC’s HandlerAdapter.

Implementation

Example test

LegacyGatewayAdapterTest