Adapter

Beginner · 13 min read · Low-Level Design, Structural Patterns

Making incompatible interfaces work together — object vs class adapters.

The idea in plain words

Your code expects one interface. A class you want to use, often from a third-party library, offers a different one. An Adapter is a small wrapper that translates between them, so neither side has to change.

The problem

Your checkout depends on your own abstraction:

The solution: object adapter

Create a class that implements your interface and wraps the incompatible object, translating calls.

Class adapter (via inheritance)

The adapter can also extend the adaptee and implement the target interface:

Two-way and anti-corruption layers

At larger scale, a set of adapters protecting your domain model from an external system's model is called an anti-corruption layer (Domain-Driven Design). It's the same idea applied to a whole subsystem: your code speaks your language; translation happens at the boundary.

Where you've seen it

When to use

Don't use an adapter when you control both sides — just change the interface. Avoid adapters that silently drop important behaviour (a vendor's partial-success semantics squeezed into a boolean).

Pros and cons

Comparison with similar patterns

Adapter is typically applied after the fact to make existing things fit; Bridge is designed up front. Adapter usually wraps one object; Facade wraps a subsystem.

Interview questions

  • What's the difference between Adapter and Facade?
  • How would you design payment integration for three providers so you can switch providers per merchant?
  • Object adapter or class adapter in Java — which and why?

Further reading

Key takeaways

  • Adapter wraps an incompatible object and exposes the interface your code expects.
  • Translate method shapes, data types/units, results and exceptions at the boundary.
  • Prefer object adapters (composition) over class adapters (inheritance).
  • Adapter changes the interface; Decorator keeps it and adds behaviour; Proxy keeps it and controls access; Facade simplifies many objects.

Previous: Prototype · Next: Decorator