GoF pattern • Structural
GoFStructuralJava example
Adapter solves a specific structural problem in the design.
Translate one interface into another that the client expects.
Pattern overview
GoF pattern
Translate one interface into another that the client expects.
Intent
Convert the interface of a class into another interface clients need.
Problem
Useful functionality already exists, but its API does not match the contract the rest of the system uses.
Solution
Wrap the existing implementation in an adapter that speaks the expected interface.
Fit and tradeoffs
FamilyGoF
CategoryStructural
Best used when
- When integrating legacy libraries or third-party APIs.
- When you want to isolate external API shape from domain code.
Tradeoffs
- Adds one more abstraction layer.
- Can become cluttered if overused for every minor mismatch.
Java example
This example is intentionally compact so the pattern shape stays easy to see.
interface PaymentGateway {
Receipt charge(Money amount);
}
final class LegacyBillingClient {
String submitCents(long cents) { return "OK-" + cents; }
}
final class LegacyBillingAdapter implements PaymentGateway {
private final LegacyBillingClient client = new LegacyBillingClient();
@Override
public Receipt charge(Money amount) {
String id = client.submitCents(amount.toCents());
return new Receipt(id);
}
}