Adapter
You'll learn to
- -Wrap an incompatible interface so it can be used where a different interface is expected
- -Recognize the trigger: code you depend on but cannot change, with the wrong shape
- -Distinguish object adapters from class adapters and know which one interview code favors
- -Tell Adapter apart from Decorator, Facade and Proxy
Imagine your checkout code calls gateway.charge(12.50) in forty places. Then the company signs a contract with a billing vendor whose library wants submit_transaction(1250, "USD"): whole cents, and the currency as a separate argument. You cannot edit the vendor's code, and you do not want forty call sites to learn about cents. The Adapter pattern is the standard answer, and it is one of the most common patterns in real codebases, because every system eventually has to talk to code it does not control.
An Analogy: The Travel Plug
A US laptop charger will not fit a European wall socket. You do not rewire the hotel, and you do not cut the plug off your charger. You put a small adapter between them: it has the shape the socket expects on one side and the shape your charger has on the other. Neither the laptop nor the wall changes.
- -The wall socket is the interface your code expects (Target): PaymentGateway.charge(dollars).
- -The foreign plug is the code you cannot change (Adaptee): LegacyBillingSDK.submit_transaction(cents, currency).
- -The travel plug is the Adapter: it implements the interface you expect and translates each call into the one the vendor wants.
The Classic Trigger: Integrating a Third-Party Library
Adapter shows up whenever your code expects one interface but a library you cannot modify exposes another. Without the pattern you have two bad options. You can scatter the translation, int(amount * 100) and the currency code, across every call site, so a vendor change means forty edits and a missed one is a billing bug. Or you can fork the vendor's library, which you will then maintain for ever. With an adapter, the translation lives in exactly one class.
class PaymentGateway(ABC): # the interface your code expects
@abstractmethod
def charge(self, amount_dollars: float) -> None: ...
class LegacyBillingSDK: # third-party code you cannot modify
def submit_transaction(self, cents: int, currency: str) -> None: ...
class LegacyBillingAdapter(PaymentGateway):
def __init__(self, sdk: LegacyBillingSDK):
self._sdk = sdk
def charge(self, amount_dollars: float) -> None:
cents = round(amount_dollars * 100) # not int(): int(0.29 * 100) is 28
self._sdk.submit_transaction(cents, "USD")Tracing One Call
- -Checkout calls gateway.charge(12.50), where gateway is typed as PaymentGateway. It has no idea an adapter is involved.
- -The adapter converts 12.50 dollars into 1250 cents.
- -It calls the vendor's submit_transaction(1250, "USD") and returns.
Notice the conversion detail in the code. int(0.29 * 100) is 28 in Python, because 0.29 * 100 is 28.999999999999996 and int() truncates. round() gives the 29 you meant. For real money, store integers or use Decimal. This is exactly the kind of detail that belongs in the adapter, in one place, instead of in forty.
A bonus appears in testing. Because callers depend on PaymentGateway, you can give your tests a FakeGateway that records charges, and the vendor's library never runs. If the vendor changes, you write one new adapter.
Object Adapter vs. Class Adapter
The version above is an object adapter: it holds a reference to the wrapped object and delegates to it. A class adapter instead uses multiple inheritance, extending both the target interface and the adaptee simultaneously. Object adapters are strongly favored in interviews and in most real codebases: they work in languages without multiple inheritance, they can adapt any subclass of the adaptee (not just the exact type inherited from), and they keep the adaptee's internals fully encapsulated rather than exposed through inheritance.
Adapter or Something Else?
Several patterns wrap an object, and interviews like to ask which is which. The question to ask is what the wrapper does to the interface:
- -Adapter: changes the interface, so that something with the wrong shape fits the shape you expect.
- -Decorator: keeps the same interface and adds behavior, such as logging or caching.
- -Facade: offers a new, simpler interface over a whole subsystem, but not necessarily one that matches an existing interface.
- -Proxy: keeps the same interface and controls access to the real object, for example lazily loading it or checking permissions.
In one line: when the code you depend on has the wrong shape and you cannot change it, wrap it in an adapter that implements your interface and translates in one place.
Interview Signal is part of Pro
See a real weak answer next to a real strong one for this exact topic.
Quiz is part of Pro
Test what you just read with a short quiz, and bank the XP.
Discussion0
Join the Discussion
Sign in to leave comments, reply to others, or like insights.
No comments yet. Be the first to start the thread!