The Adapter Pattern helps two classes with incompatible interfaces work together. It acts like a bridge, converting one interface into another so the client can use it without changing the existing code.
This is especially useful when you want to:
-
Connect legacy code to a new system
-
Use third-party libraries that don’t match your current interface
-
Avoid rewriting existing code while making it compatible
In simple terms, the Adapter Pattern makes two systems talk to each other smoothly without modifying either one.
Examples of the Adapter Pattern
-
Travel Adapter: When you go abroad, your phone charger might not fit the local sockets. Instead of buying a new charger, you use a travel adapter to make it work. The adapter connects your charger to the foreign socket without changing the charger itself.
-
Charger Converter: If your phone uses a Type-C port but you only have a Type-B cable, you don’t replace your phone. You use a converter to connect them. The converter acts as an adapter, letting incompatible devices work together.
In both cases, the adapter bridges the gap between two incompatible things without changing either one.
UML Diagram

Why It Matters
In real-world software, you often need to work with third-party APIs or old systems. These systems might send data in a format your app doesn’t understand. For example, your app expects JSON, but the service returns XML. You can’t change the third-party system, and rewriting your app would be too much work.
The solution is an Adapter that translates between the two formats.
Real-World Example: XML to JSON Adapter
Scenario:
A third-party service provides data in XML, but your application only works with JSON.
Solution:
You create an XmlToJsonAdapter. It takes the XML from the service, converts it to JSON, and passes it to your app. The app and the service don’t need to change.
How It Works (Simplified)
-
Target Interface: What your app expects (JSON).
-
Adaptee: The third-party service (provides XML).
-
Adapter: Wraps the Adaptee and converts XML into JSON so your app can use it.
This way, incompatible systems can work together without changing their core logic.
UML Diagram

Pros of the Adapter Pattern
-
Lets you reuse existing code without changing it.
-
Keeps classes focused on their main tasks, while the adapter handles compatibility.
-
Can support multiple interfaces using different adapters.
-
Decouples the system from specific implementations, making future changes easier.
Cons of the Adapter Pattern
-
Adds extra complexity, which can make the code harder to read.
-
Slight performance overhead because of the additional layer.
-
Having many adapters can make maintenance harder.
-
Overusing adapters for small changes can unnecessarily complicate the design.
When to Use the Adapter Pattern
-
When you need incompatible systems to work together.
-
To reuse existing code or libraries without rewriting them.
-
To integrate new components while keeping the system flexible.
-
To centralize compatibility changes, making maintenance safer and easier.
When Not to Use
-
If your system is simple and all components already fit together, an adapter isn’t needed.
-
In performance-critical applications, where even slight overhead matters.
-
If there’s no interface mismatch, using an adapter is unnecessary.
-
For short-term projects, the effort of creating adapters may not be worth it.
Different Types of Adapter Pattern
The Adapter Pattern can be implemented in several ways depending on the situation and the programming language:
-
Class Adapter (Inheritance-based)
-
The adapter inherits from both the target interface (what the client expects) and the adaptee (the existing class that needs adaptation).
-
Works in languages that support multiple inheritance, like C++.
-
Less common in Java or C# because they don’t allow multiple inheritance.
-
-
Object Adapter (Composition-based)
-
The adapter contains an instance of the adaptee and implements the target interface.
-
More flexible because one adapter can work with multiple adaptees.
-
Common in Java and C#.
-
-
Two-way Adapter
-
Works as both a target and an adaptee depending on which side is calling.
-
Useful when two systems need to communicate and adapt to each other.
-
-
Interface Adapter (Default Adapter)
-
When an interface has many methods but you only need a few, the adapter provides default implementations for the unused methods.
-
Often used in Java with abstract classes or default interface methods.
-
How the Adapter Pattern Works
-
The client calls a method on the adapter through the target interface.
-
The adapter converts the client’s request into a format the adaptee understands.
-
The adaptee performs the actual work.
-
The client receives the result without knowing about the adapter or the adaptee’s details.
This allows incompatible systems to work together smoothly without changing their core code.
Summary
The Adapter Pattern acts like a bridge between two incompatible systems, allowing them to work together without changing their existing code. It converts one interface into another, making it useful for connecting legacy code, third-party libraries, or different data formats. For example, an XML-to-JSON adapter lets your app work with a service that provides XML, even if your app expects JSON. The adapter wraps the original system, handles the conversion, and keeps everything else unchanged, making the design flexible and reusable while avoiding direct modifications.
Code
By Coder Army