Adapter
Also known as: Wrapper
Let objects with incompatible interfaces work together, through a translator.
- 4 animated steps
- 6 languages
- Structural patterns
The problem
Your checkout expects a PaymentGateway.pay(euros) interface. The third-party SDK (Stripe, ClicToPay…) exposes charge(cents, currency) and you cannot modify it.
The solution
Create an Adapter class that implements the expected interface and translates each call to the third-party API (names, formats, units, errors).
The pattern in action
- Client
- Key role of the pattern
- Object
- Data or state
- The client speaks the interface it knows: PaymentGateway.
- The adapter translates: euros → cents, method name, parameters.
- The SDK replies in its own format…
- …which the adapter converts back into the format the client expects.
An analogy
A power plug adapter: your European plug works in a British socket without modifying either one.
When to use it
- Integrate a third-party library / API
- Migrate a legacy system gradually
- Unify several providers behind a single interface
Pros and cons
Pros
- Business code isolated from external APIs
- Switching providers = a new adapter
- Follows SRP and Open/Closed
Cons
- One more layer of indirection
- Can hide important behavioral differences
Implementations
The same example, written idiomatically in each language.
Adapter in Python
from typing import Protocol
class PaymentGateway(Protocol):
def pay(self, amount_eur: float) -> bool: ...
class StripeSDK: # third-party API, cannot be modified
def charge(self, cents: int, currency: str) -> dict:
return {"status": "succeeded"}
class StripeAdapter:
def __init__(self, sdk: StripeSDK):
self.sdk = sdk
def pay(self, amount_eur: float) -> bool:
res = self.sdk.charge(round(amount_eur * 100), "eur")
return res["status"] == "succeeded"
def checkout(gateway: PaymentGateway) -> bool:
return gateway.pay(49.90)
checkout(StripeAdapter(StripeSDK()))
Adapter in TypeScript
interface PaymentGateway {
pay(amountEur: number): Promise<boolean>;
}
class StripeSDK { // third-party API
async charge(cents: number, currency: string) {
return { status: "succeeded" };
}
}
class StripeAdapter implements PaymentGateway {
constructor(private sdk: StripeSDK) {}
async pay(amountEur: number) {
const res = await this.sdk.charge(Math.round(amountEur * 100), "eur");
return res.status === "succeeded";
}
}
const gateway: PaymentGateway = new StripeAdapter(new StripeSDK());
await gateway.pay(49.9);
Adapter in Java
interface PaymentGateway { boolean pay(double amountEur); }
class StripeSdk { // third-party API
Map<String, String> charge(long cents, String currency) {
return Map.of("status", "succeeded");
}
}
class StripeAdapter implements PaymentGateway {
private final StripeSdk sdk;
StripeAdapter(StripeSdk sdk) { this.sdk = sdk; }
@Override
public boolean pay(double amountEur) {
var res = sdk.charge(Math.round(amountEur * 100), "eur");
return "succeeded".equals(res.get("status"));
}
}
Adapter in C#
interface IPaymentGateway { bool Pay(decimal amountEur); }
class StripeSdk // third-party API
{
public Dictionary<string, string> Charge(long cents, string currency) =>
new() { ["status"] = "succeeded" };
}
class StripeAdapter(StripeSdk sdk) : IPaymentGateway
{
public bool Pay(decimal amountEur)
{
var res = sdk.Charge((long)Math.Round(amountEur * 100), "eur");
return res["status"] == "succeeded";
}
}
Adapter in PHP
interface PaymentGateway { public function pay(float $amountEur): bool; }
class StripeSdk // third-party API
{
public function charge(int $cents, string $currency): array
{
return ['status' => 'succeeded'];
}
}
final class StripeAdapter implements PaymentGateway
{
public function __construct(private StripeSdk $sdk) {}
public function pay(float $amountEur): bool
{
$res = $this->sdk->charge((int) round($amountEur * 100), 'eur');
return $res['status'] === 'succeeded';
}
}
// Laravel: $this->app->bind(PaymentGateway::class, StripeAdapter::class);
Adapter in Go
type PaymentGateway interface {
Pay(amountEur float64) bool
}
type StripeSDK struct{} // third-party API
func (StripeSDK) Charge(cents int64, currency string) map[string]string {
return map[string]string{"status": "succeeded"}
}
type StripeAdapter struct{ sdk StripeSDK }
func (a StripeAdapter) Pay(amountEur float64) bool {
res := a.sdk.Charge(int64(math.Round(amountEur*100)), "eur")
return res["status"] == "succeeded"
}
var _ PaymentGateway = StripeAdapter{} // compile-time check
Frequently asked questions
What is the Adapter design pattern?
Let objects with incompatible interfaces work together, through a translator. Create an Adapter class that implements the expected interface and translates each call to the third-party API (names, formats, units, errors).
When should you use the Adapter pattern?
Integrate a third-party library / API; Migrate a legacy system gradually; Unify several providers behind a single interface.
What are the pros and cons of the Adapter pattern?
Pros: Business code isolated from external APIs; Switching providers = a new adapter; Follows SRP and Open/Closed. Cons: One more layer of indirection; Can hide important behavioral differences.