Simple definition#
The Adapter pattern lets code work with a class whose interface does not match what the code expects. The adapter implements the interface the client wants and translates every call into the interface the existing class provides.
GoF intent: “Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.” It is also known as Wrapper.
The problem#
A checkout service needs to charge customers. The company has a contract with a payment provider whose Java SDK, LegacyPayClient, was designed years ago with conventions that clash with the rest of the codebase:
| Our code expects | The SDK provides |
|---|---|
BigDecimal amounts in major units (49.90) | long amounts in minor units (4990 cents) |
| A typed result: approved or declined | An int status code (0, 51, 54) |
| Unchecked, domain-level exceptions | A checked LegacyPayException |
| The transaction ID in the result | A separate getLastReference() call |
If CheckoutService calls the SDK directly, these conventions spread through business code. Tests need the real SDK. Switching provider later would touch every caller.
Real-world analogy#
A travel plug adapter. Your laptop charger (the client) expects a particular socket shape (the target interface). The hotel wall socket (the adaptee) provides electricity in a different shape. The adapter changes the shape of the connection without changing the laptop or the wall.
Structure#
- Target — the interface the client uses, defined in the client’s terms (
PaymentGateway). - Client — code that uses the target (
CheckoutService). - Adaptee — the existing class with the incompatible interface (
LegacyPayClient). - Adapter — implements the target and delegates to the adaptee (
LegacyPayAdapter).
classDiagram
direction LR
class CheckoutService {
-PaymentGateway gateway
+pay(String, BigDecimal) String
}
class PaymentGateway {
<<interface>>
+charge(String, BigDecimal, String) PaymentResult
}
class LegacyPayAdapter {
-LegacyPayClient client
+charge(String, BigDecimal, String) PaymentResult
-toMinorUnits(BigDecimal, String)$ long
}
class LegacyPayClient {
+makePayment(String, long, String) int
+getLastReference() String
}
CheckoutService --> PaymentGateway : uses
PaymentGateway <|.. LegacyPayAdapter
LegacyPayAdapter --> LegacyPayClient : delegates tosequenceDiagram
participant C as CheckoutService
participant A as LegacyPayAdapter
participant SDK as LegacyPayClient
C->>A: charge("tok_visa", 49.90, "EUR")
A->>A: toMinorUnits(49.90, "EUR") = 4990
A->>SDK: makePayment("tok_visa", 4990, "EUR")
SDK-->>A: 0 (OK)
A->>SDK: getLastReference()
SDK-->>A: "LP-1001"
A-->>C: Approved("LP-1001")Java implementation#
The code is in examples/java/src/hub/patterns/adapter/, compiled and run by CI with Java 21.
Target: the interface we want#
package hub.patterns.adapter;
import java.math.BigDecimal;
/** Target: the interface our application wants to use, in our own terms. */
public interface PaymentGateway {
PaymentResult charge(String customerToken, BigDecimal amount, String currency);
sealed interface PaymentResult {
record Approved(String transactionId) implements PaymentResult { }
record Declined(String reason) implements PaymentResult { }
}
}The result is a sealed interface with two records. The compiler can therefore check that every switch over a result handles both cases (Java 21 pattern matching for switch).
Adaptee: the SDK we cannot change#
package hub.patterns.adapter;
/**
* Adaptee: a third-party SDK we cannot change. Its conventions differ from
* ours: amounts in minor units (cents), numeric status codes, a checked
* exception, and a transaction reference that must be fetched separately.
*/
public class LegacyPayClient {
public static final int OK = 0;
public static final int INSUFFICIENT_FUNDS = 51;
public static final int CARD_EXPIRED = 54;
public static class LegacyPayException extends Exception {
public LegacyPayException(String message) {
super(message);
}
}
private int counter = 1000;
private String lastReference;
public int makePayment(String cardToken, long amountInCents, String currencyIso) throws LegacyPayException {
if (cardToken == null || cardToken.isEmpty()) {
throw new LegacyPayException("E-001 missing token");
}
if (cardToken.startsWith("expired")) {
return CARD_EXPIRED;
}
if (amountInCents > 50_000) { // this simulated account has 500.00 available
return INSUFFICIENT_FUNDS;
}
lastReference = "LP-" + (++counter);
return OK;
}
public String getLastReference() {
return lastReference;
}
}Adapter#
package hub.patterns.adapter;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Currency;
import java.util.Objects;
/**
* Object adapter: implements our Target interface and translates each call
* into the adaptee's API — units, status codes, exceptions and naming.
*/
public final class LegacyPayAdapter implements PaymentGateway {
private final LegacyPayClient client;
public LegacyPayAdapter(LegacyPayClient client) {
this.client = Objects.requireNonNull(client);
}
@Override
public PaymentResult charge(String customerToken, BigDecimal amount, String currency) {
long minorUnits = toMinorUnits(amount, currency);
try {
int status = client.makePayment(customerToken, minorUnits, currency);
return switch (status) {
case LegacyPayClient.OK -> new PaymentResult.Approved(client.getLastReference());
case LegacyPayClient.INSUFFICIENT_FUNDS -> new PaymentResult.Declined("insufficient funds");
case LegacyPayClient.CARD_EXPIRED -> new PaymentResult.Declined("card expired");
default -> new PaymentResult.Declined("unknown status " + status);
};
} catch (LegacyPayClient.LegacyPayException e) {
// Translate the vendor's checked exception into our own vocabulary.
throw new PaymentGatewayException("Payment provider rejected the request", e);
}
}
private static long toMinorUnits(BigDecimal amount, String currency) {
int digits = Currency.getInstance(currency).getDefaultFractionDigits();
return amount.setScale(digits, RoundingMode.UNNECESSARY).movePointRight(digits).longValueExact();
}
public static final class PaymentGatewayException extends RuntimeException {
public PaymentGatewayException(String message, Throwable cause) {
super(message, cause);
}
}
}Client#
package hub.patterns.adapter;
import java.math.BigDecimal;
/** Client: depends only on the Target interface, never on LegacyPayClient. */
public final class CheckoutService {
private final PaymentGateway gateway;
public CheckoutService(PaymentGateway gateway) {
this.gateway = gateway;
}
public String pay(String token, BigDecimal amount) {
return switch (gateway.charge(token, amount, "EUR")) {
case PaymentGateway.PaymentResult.Approved a -> "paid, transaction " + a.transactionId();
case PaymentGateway.PaymentResult.Declined d -> "declined: " + d.reason();
};
}
}Running it#
package hub.patterns.adapter;
import java.math.BigDecimal;
public final class AdapterDemo {
public static void main(String[] args) {
PaymentGateway gateway = new LegacyPayAdapter(new LegacyPayClient());
CheckoutService checkout = new CheckoutService(gateway);
System.out.println(checkout.pay("tok_visa", new BigDecimal("49.90")));
System.out.println(checkout.pay("tok_visa", new BigDecimal("899.00")));
System.out.println(checkout.pay("expired_tok", new BigDecimal("10.00")));
try {
checkout.pay("", new BigDecimal("10.00"));
} catch (LegacyPayAdapter.PaymentGatewayException e) {
System.out.println("error: " + e.getMessage() + " (cause: " + e.getCause().getMessage() + ")");
}
}
}paid, transaction LP-1001
declined: insufficient funds
declined: card expired
error: Payment provider rejected the request (cause: E-001 missing token)Step-by-step walkthrough#
- Define the target in your own terms first.
PaymentGatewayis designed for what checkout needs, not copied from the SDK. This is the most important decision: a target that mirrors the adaptee adapts nothing. - Translate units safely.
toMinorUnitsasksjava.util.Currencyhow many fraction digits the currency has — 2 for EUR, 0 for JPY.RoundingMode.UNNECESSARYthrows if the amount has more decimals than the currency allows, instead of silently rounding money. - Translate status codes into types. The magic numbers
0,51and54are mapped toApprovedandDeclined. An unknown code becomes a decline with a diagnostic reason, not an exception that would crash checkout. - Translate exceptions. The SDK’s checked
LegacyPayExceptionis wrapped inPaymentGatewayException, keeping the original as the cause for debugging. Callers no longer depend on vendor exception types. - Hide awkward call sequences. The SDK needs a second call,
getLastReference(), to obtain the transaction ID. The adapter combines both calls into one result. - The client never sees the adaptee.
CheckoutServicedepends only onPaymentGateway. In tests it can receive a lambda or a stub instead of the SDK.
Object adapter versus class adapter#
The GoF book describes two forms:
| Object adapter | Class adapter | |
|---|---|---|
| Mechanism | Holds a reference to the adaptee (composition) | Extends the adaptee and implements the target (inheritance) |
| Works with subclasses of the adaptee | Yes — any instance can be passed in | No — bound to one concrete class |
| Can override adaptee behaviour | No | Yes |
| Exposes the adaptee’s public methods | No | Yes, unintentionally |
| Usual choice in Java | Yes | Rarely |
Java allows only one superclass, and a class adapter leaks the adaptee’s methods to every caller. The object adapter is the default choice; this example uses it.
Advantages and disadvantages#
| Advantages | Disadvantages |
|---|---|
| Reuses existing code you cannot or should not change | An extra class and a level of indirection |
| Keeps vendor conventions out of business code | Translation can be lossy; some adaptee features may not fit the target |
| Clients can be tested with simple fakes of the target | Several adapters around one adaptee can duplicate translation logic |
| Swapping the vendor means writing a new adapter only | A poorly designed target just moves the mess into the adapter |
When to use it#
- Integrating a third-party library, legacy module or external API whose interface does not match yours.
- Isolating code from a dependency that is likely to change, such as a payment or messaging provider.
- Implementing the outbound ports of Hexagonal Architecture, where every outbound adapter is an Adapter in the GoF sense.
- Making several similar services, such as two payment providers, usable through one interface.
When not to use it#
- You own both sides and can simply change one of the interfaces.
- The interfaces already match. A pass-through wrapper adds indirection without value.
- You need to add behaviour, not translate it — that is a Decorator. To simplify a whole subsystem, use a Facade. See the comparison.
Common implementation mistakes#
- Leaking adaptee types. Returning SDK objects or throwing SDK exceptions through the target defeats the isolation.
- Silent lossy conversions. Rounding money, truncating strings or ignoring unknown status codes without a trace creates bugs that are hard to diagnose.
- Business rules in the adapter. The adapter should translate, not decide. A rule such as “retry declined payments twice” belongs in the application, not in the vendor wrapper.
- Untested adapters. Adapters contain real logic — unit conversion, code mapping — and need their own tests against the adaptee or a contract test.
Relationship to SOLID#
- Dependency Inversion: the client depends on an abstraction it owns (
PaymentGateway), not on the vendor’s concrete class. - Single Responsibility: translation is the adapter’s only job.
- Open/Closed: a new provider means a new adapter;
CheckoutServicedoes not change. - Interface Segregation: the target exposes only what clients need, not every SDK method.
Comparison with similar patterns#
| Pattern | Interface relative to the wrapped object | Purpose |
|---|---|---|
| Adapter | Different | Make an incompatible interface usable |
| Decorator | Same | Add behaviour, can be stacked |
| Proxy | Same | Control access: lazy loading, security, remote calls, transactions |
| Facade | New, simpler | One entry point to a whole subsystem |
| Bridge | Designed up front | Separate abstraction from implementation before they diverge; Adapter fixes the mismatch afterwards |
A full side-by-side treatment, with code, is in Adapter vs Decorator vs Proxy vs Facade.
At the architecture level, Domain-Driven Design’s Anti-Corruption Layer applies the same idea to a whole model: it translates another bounded context’s language into yours. It is usually built from adapters, facades and translators.
Real-world uses#
java.util.Arrays.asList(array)presents an array as aList.java.io.InputStreamReaderadapts a byte stream (InputStream) to a character stream (Reader).java.util.Collections.enumeration(collection)adapts aCollectionto the legacyEnumerationinterface.- Spring MVC’s
HandlerAdapterlets theDispatcherServletinvoke different kinds of handler through one interface. - Every outbound adapter in the Hexagonal Architecture example.
Interview questions and answers#
What is the Adapter pattern? A structural pattern that converts the interface of an existing class into the interface a client expects. The adapter implements the target interface and delegates to the adaptee, translating calls, data and errors.
What is the difference between an object adapter and a class adapter? An object adapter holds a reference to the adaptee. A class adapter inherits from it. In Java, object adapters are preferred: they work with any subclass of the adaptee and do not expose its methods.
How is Adapter different from Decorator and Proxy? All three wrap another object. An adapter changes the interface. A decorator keeps the interface and adds behaviour. A proxy keeps the interface and controls access to the real object.
Where do you see adapters in Hexagonal Architecture? Every adapter that implements an outbound port is a GoF Adapter: it translates the application’s port interface into a technology’s API, such as JPA or an HTTP client. Inbound adapters, such as REST controllers, translate the other way.
What should an adapter not do? It should not contain business rules. It translates; decisions belong in the application or domain layer.
Summary#
- Adapter makes an existing class usable through an interface it does not implement.
- Design the target interface around the client’s needs; the adapter absorbs the mismatch.
- Prefer object adapters (composition) in Java.
- Adapters translate; they do not add behaviour (Decorator), control access (Proxy) or simplify subsystems (Facade).
Related topics#
- Adapter vs Decorator vs Proxy vs Facade
- Layered versus Hexagonal Architecture
- What Is a Bounded Context? — and the Anti-Corruption Layer
- All 23 GoF Patterns: Overview
References#
- Gamma, Helm, Johnson, Vlissides. Design Patterns. Addison-Wesley, 1994. Adapter, p. 139.
- Eric Evans. Domain-Driven Design. Addison-Wesley, 2003. Chapter 14: Anti-Corruption Layer.
java.util.Currency, Java SE 21 API.- JEP 441: Pattern Matching for switch (final in Java 21).