Adapter Design Pattern in Java with Real Example
Understand the Adapter Design Pattern in Java using real-world examples like third party payment APIs. Learn how to bridge incompatible interfaces cleanly.
Adapter Design Pattern

The Adapter Design Pattern helps two things work together even if they don't naturally fit. It acts like a translator, converting one type of interface into the format the other one understands.
Real Life Examples
Payments
Let’s imagine a practical scenario.
You’ve built a website with some premium features. To accept payments, you explore different payment gateways. After your research, you discover:
-
Stripe has lower fees for international (especially US) customers.
-
Razorpay is cheaper for Indian users.
So, you decide to support both Stripe and Razorpay.
Here’s where things get tricky.
-
Stripe’s SDK provides a method like this:
makePayment(cardNumber, amountInUSD) -
Razorpay’s API expects something different:
createPayment(phoneNumber, amountInPaise)
This means:
-
You’ll have to write if-else checks everywhere to decide which payment gateway to use.
-
These APIs are third-party so they can change their method names or signatures anytime, which would break your code.
To solve this and make your code neat and future-proof, we can use the Adapter Design Pattern.
We’ll create a common interface called pay(amountInUSD). No matter what gateway is selected - Stripe or Razorpay, you’ll always call the same method: pay(amountInUSD).
Also, these are third-party APIs. If they suddenly change their method names or parameters, your app will break and you'll need to update your code everywhere you’ve used it.
The adapter will handle the conversion behind the scenes.
So whether the API takes amount in dollars, rupees, or even bananas 🍌 - you don’t care! Your system just calls pay(amountInUSD) and the adapter takes care of the rest.

This is where the user selects a payment method. Based on that, we use the appropriate adapter.
As you can see, no matter which gateway is selected, your code just calls one method: pay(amountInUSD).
Data Format Conversion
Now let’s talk about another scenario.
But now, you need to use an older service - maybe built 10 years ago—that still returns data in XML format:
You don’t want your app to handle both JSON and XML (that would make your code messy). So, you create an adapter that takes XML and converts it into JSON. Problem solved.

The Adapter Wraps around the old service and converts data to JSON:
UML Diagram
Let’s quickly go over the main players in this design pattern:
-
Adapter is the middleman which translates the incompatible code into a format your system understands.
-
Adaptee is existing/legacy/third-party code you're trying to use.
-
Target Interface is the format or interface that your app wants everything to follow.
When Should You Use the Adapter Pattern?
-
You want to work with third-party APIs, but they have different method names or data formats.
-
You’re dealing with legacy code that you cannot change.
-
You want to offer your system a unified interface so it’s easier to maintain and update in the future.