What is adapter?

An adapter is a piece of code (or a small device) that lets two incompatible things work together by translating one interface into another. In software, the Adapter pattern wraps an existing class with a new interface that a client expects, so the client can use the class without changing its own code.

Let's break it down

  • Client: the part of the program that wants to use some functionality.
  • Adaptee: the existing class that has the needed functionality but a different interface.
  • Adapter: a thin wrapper that implements the client’s expected interface and internally calls the adaptee’s methods, converting data or calls as needed.

Why does it matter?

Adapters let you reuse old or third‑party code without rewriting it, keep your own code clean and stable, and make it easier to swap components later. They help you integrate different libraries, APIs, or hardware without breaking existing logic.

Where is it used?

  • Connecting a new payment gateway API to an e‑commerce platform.
  • Using a legacy logging library in a modern application that expects a different logging interface.
  • Plugging a USB‑C device into a laptop that only has a USB‑A port (hardware version).
  • Adapting different data formats (e.g., XML to JSON) for a service that only reads one format.

Good things about it

  • Promotes code reuse and reduces duplication.
  • Isolates changes: only the adapter needs updating if the adaptee changes.
  • Improves flexibility; you can switch out the adaptee with another implementation easily.
  • Keeps the client code simple and focused on its own responsibilities.

Not-so-good things

  • Adds an extra layer, which can slightly impact performance.
  • May increase the amount of code you have to maintain, especially if many adapters are needed.
  • If overused, it can make the system harder to understand because of many indirections.