teacher
Sign in

Adapter

Make a class with the wrong interface fit the one your code expects, without editing either side.

20 min3 interactive

The problem it solves

You depend on code you cannot change: a vendor SDK (software development kit), a legacy module, a library. Its methods do the right job but have the wrong shape. Your code expects pay(orderId, amountPaise) returning boolean. The vendor gives makePayment(ref, rupees) returning an int status code.

Options:

  • Edit the vendor code. Impossible, or lost on the next upgrade.
  • Scatter conversions (paise / 100.0, code == 0) through your code. Every caller now knows vendor details.
  • Write one adapter: a small class that implements the interface you want and translates calls to the interface you have.

Think of a travel plug adapter. The socket and the charger stay as they are.

1 of 8