Structural Wrapper

Adapter

Adapter is a structural pattern that wraps an object with a mismatched interface behind a translator, letting client code call it as if it spoke the expected language all along.

Complexity
Popularity

Problem

A payment module in your checkout flow was built around a `charge(amountCents, cardToken)` method, and every screen in the app calls it that way. Now the business wants to add a new payment provider whose SDK only exposes `submitTransaction({ total, currency, source })`.

You can't rename the SDK's method - it's someone else's package, updated independently. Rewriting your entire checkout flow to match the new SDK's shape would touch dozens of call sites for the sake of one provider. And the next provider you integrate will likely have yet another shape, repeating the problem.

Solution

Introduce a thin wrapper class that implements the interface your code already expects, and internally translates each call into whatever the external object actually needs. The wrapper receives calls in the familiar shape, reformats the arguments, invokes the wrapped object, and reshapes the result back if needed.

Neither side has to change: the caller keeps using the interface it always used, and the wrapped object keeps its own interface untouched. All the translation logic - renaming fields, converting units, reordering parameters - lives in one isolated place instead of being smeared across the codebase.

When to Use

  • You need an existing class to work with your code, but its method names, argument order, or data format don't line up with what your code expects.
  • You're integrating a third-party SDK or legacy module you cannot edit, and don't want its interface leaking into the rest of the application.
  • Several related classes provide similar functionality through slightly different interfaces, and you want to unify them under one shape.

Real-World Examples

  • **Travel power plug adapters** - let a device built for one country's socket shape connect to a different country's outlet without altering the device.
  • **Java's `Arrays.asList()`** - wraps a plain array so it can be handed to any code expecting the `List` interface, without copying the underlying data.
  • **Payment gateway SDK wrappers** - normalize Stripe's, PayPal's, and Adyen's differently-shaped charge APIs behind one internal `PaymentProvider` interface.