Adapter Pattern in Python
Make an old or third-party class fit the interface your code expects, without changing that class.
- Incompatible interfaces
- Wrapping the old object
- Translating calls and data
- 1
client.pay(25)The interface the client knows - 2
PaymentAdapterTranslates the call - 3
legacy.make_payment(...)The old interface - 4
PAY 25.00Result
A translator between two interfaces
The problem
You often have to use code you cannot change, such as an old payment system or a third-party library, whose methods do not match what the rest of your program expects. Rewriting every caller is risky. An adapter translates between the two.
Think of it like this
An adapter is like a travel plug adapter. Your laptop plug fits your home sockets, and the hotel's sockets are a different shape. The adapter sits between them, so neither the laptop nor the wall has to change.
gateway.pay(25)🔌 Your plug: pay in dollarsPaymentAdapter🔁 The travel adapter in betweenround(dollars * 100)💱 Convert dollars to centsmake_payment(cents)🧱 The old socket: wants centsThe Adapter pattern wraps an object with an incompatible interface in a class that offers the interface the client expects and translates each call.
Key points
LegacyGatewayonly offersmake_payment(cents).PaymentAdapterofferspay(dollars), the method the rest of the program wants to call.round(dollars * 100)converts dollars to cents before callingmake_payment.gateway.pay(25)works without changingLegacyGatewayat all.
When should you use it?
- Using old or third-party code behind the interface your program expects.
- Switching between providers, such as payment or email services, without changing callers.
- Converting units or data formats at the boundary.
When not to use it
- You own both sides and can simply rename or change one of them.
- The interfaces differ so much that the adapter would contain most of the logic: redesign the boundary instead.
Example code
class LegacyGateway:
# Adapter: wrap an incompatible API with the expected interface.
def make_payment(self, cents):
return f"PAY {cents / 100:.2f}"
class PaymentAdapter:
def __init__(self, gateway):
self.gateway = gateway
def pay(self, dollars):
# Translate both the method name and amount format.
return self.gateway.make_payment(round(dollars * 100))
gateway = PaymentAdapter(LegacyGateway())
print(gateway.pay(25))
Result of running the code
PAY 25.00
Code explanation
LegacyGateway expects amounts in cents through make_payment, while the rest of the program wants to call pay with dollars. PaymentAdapter stores the old gateway and implements pay by converting dollars to cents and calling make_payment. The caller uses pay(25), and the legacy class receives 2500 without being modified.
Change the values and run the program in the CodeUtility online Python compiler without installing Python locally.
Practice exercises
Modify the runnable example with the exercises below to build understanding beyond copying the result.
- Replace the sample domain while preserving the pattern structure.
- Compare the pattern with a simpler implementation and explain the tradeoff.
- Write tests for each participant before adding another implementation.
Common mistakes
Forgetting to convert the values
Renaming the method alone sends dollars where cents are expected.
def pay(self, dollars):
return self.gateway.make_payment(dollars)
def pay(self, dollars):
return self.gateway.make_payment(round(dollars * 100))
Finished this lesson?
Mark it as learned to track your progress.
Practice with a challenge
Use what this lesson covers to solve a real problem. Each challenge runs in your browser with instant tests and an AI review.
All challenges