Advertisement
Advanced JavaScript, Design Patterns in JavaScript

What is the Adapter pattern and why is it useful when integrating third-party code?

Short answer

Adapter wraps an incompatible interface with a new one that matches what your code expects, letting you swap or update the underlying library without rewriting all the code that depends on it.

Save JavaScript mock interview
Advertisement
AModel answer
Adapter wraps an incompatible interface with a new one that matches what your code expects, letting you swap or update the underlying library without rewriting all the code that depends on it.
class LegacyLogger {
  logMessage(msg) { console.log('LEGACY:', msg); }
}
class LoggerAdapter {
  #legacy = new LegacyLogger();
  log(msg) { this.#legacy.logMessage(msg); } // matches modern interface
}
Real-world example
Wrapping an old third-party analytics SDK so the rest of the app can call a consistent, modern log() API.
Likely follow-ups
  • How does Adapter differ from Facade, which also 'wraps' something?
Related topics