Skip to content

Adapter

Allow objects with incompatible interfaces to collaborate.

A travel adapter lets you plug a US plug (flat pins) into a European socket (round holes). The adapter converts one interface to another.


flowchart LR
Client --> Target[Target Interface]
Target --> Adapter[Adapter]
Adapter --> Adaptee[Adaptee<br/>Incompatible Interface]
style Client fill:#3b82f6,color:#fff
style Target fill:#7c3aed,color:#fff
style Adapter fill:#f59e0b,color:#fff
style Adaptee fill:#ef4444,color:#fff

❌ Bad Example — Directly Using Incompatible Code

Section titled “❌ Bad Example — Directly Using Incompatible Code”
// Old system — returns XML
class OldWeatherAPI {
getXML() {
return '<weather><temp>25</temp></weather>';
}
}
// New system — expects JSON
class WeatherApp {
display(data) {
console.log(`Temperature: ${data.temp}°C`);
}
}
// ❌ Can't directly use OldWeatherAPI with WeatherApp
const oldAPI = new OldWeatherAPI();
const app = new WeatherApp();
// app.display(oldAPI.getXML()); // Won't work — expects JSON

// Target interface
class WeatherApp {
display(data) {
console.log(`Temperature: ${data.temp}°C`);
}
}
// Adaptee (incompatible)
class OldWeatherAPI {
getXML() {
return '<weather><temp>25</temp></weather>';
}
}
// Adapter
class WeatherAdapter {
constructor(oldAPI) {
this.oldAPI = oldAPI;
}
getJSON() {
const xml = this.oldAPI.getXML();
// Simple XML to JSON conversion
const temp = xml.match(/<temp>(\d+)<\/temp>/)[1];
return { temp: parseInt(temp) };
}
}
// Usage
const oldAPI = new OldWeatherAPI();
const adapter = new WeatherAdapter(oldAPI);
const app = new WeatherApp();
app.display(adapter.getJSON()); // "Temperature: 25°C" ✅

  • You have an existing class with an incompatible interface
  • You want to use a third-party library without changing your code
  • You need to wrap legacy code behind a modern interface
  • You can modify the source code — just change the interface directly
  • The interfaces are already compatible

  • Wraps an incompatible object so it looks like what the client expects
  • Like a power adapter between two different plug types
  • Lets old and new code work together without changing either