Sitemap
JavaScript-World

JavaScript-World

Elevating web experiences with 10+ yrs in JS🚀.Crafting seamless UIs,optimizing performance, mentoring next-gen coders.Passionate about learning&innovation.🌐✨

JavaScript Adapter Design Pattern

2 min readJun 5, 2023

--

Press enter or click to view image in full size
Photo by Ferenc Almasi on Unsplash

The Adapter Pattern is a design pattern that allows you to bridge the gap between incompatible interfaces or classes, enabling them to work together seamlessly. This pattern is particularly useful in JavaScript development, as it promotes code reusability and simplifies the integration of third-party libraries or APIs.

In this blog post, we will explore the Adapter Pattern in detail, along with examples to demonstrate its implementation in JavaScript.

What is the Adapter Pattern?

The Adapter Pattern involves creating a new interface that is compatible with an existing one, allowing objects with incompatible interfaces to communicate and work together. This pattern promotes the decoupling of the object that uses the adapter from the object that provides the functionality, improving code maintainability.

Implementing the Adapter Pattern in JavaScript

Let’s dive into a simple example to understand how the Adapter Pattern can be implemented in JavaScript.

class OldCalculator {
constructor() {
this.operations = function(term1, term2, operation) {
switch (operation) {
case 'add':
return term1 + term2;
case 'sub':
return term1 - term2;
default:
return NaN;
}
};
}
}
class NewCalculator {
constructor() {
this.add = function(term1, term2) {
return term1 + term2;
};
this.sub = function(term1, term2) {
return term1 - term2;
};
}
}
class CalculatorAdapter {
constructor() {
const newCalculator = new NewCalculator();
this.operations = function(term1, term2, operation) {
switch (operation) {
case 'add':
return newCalculator.add(term1, term2);
case 'sub':
return newCalculator.sub(term1, term2);
default:
return NaN;
}
};
}
}
const oldCalc = new OldCalculator();
const adaptedCalc = new CalculatorAdapter();
console.log(oldCalc.operations(3, 2, 'add')); // Output: 5
console.log(adaptedCalc.operations(3, 2, 'add')); // Output: 5

In the example above, we define two calculator classes: OldCalculator and NewCalculator. The OldCalculator class uses a single method, operations, to perform addition and subtraction. The NewCalculator class, on the other hand, has separate methods for addition and subtraction (add and sub, respectively).

Get JavaScript-World’s stories in your inbox

Join Medium for free to get updates from this writer.

We create an CalculatorAdapter class that uses the NewCalculator class internally and exposes an operations method, similar to the OldCalculator class. This allows objects that expect the OldCalculator interface to work seamlessly with the NewCalculator class.

Conclusion

The Adapter Pattern is a powerful design pattern that allows you to create compatibility between objects with different interfaces, promoting code reusability and simplifying the integration of third-party libraries or APIs. By implementing this pattern in your JavaScript projects, you can create maintainable and adaptable code that is easy to extend and manage.

If you enjoyed the article and would like to show your support, be sure to:

👏 Applaud for the story (50 claps) to help this article get featured

👉Follow me on Medium

Check out more content on my Medium profile

--

--

JavaScript-World

Written by JavaScript-World

Elevating web experiences with 10+ yrs in JS🚀.Crafting seamless UIs,optimizing performance, mentoring next-gen coders.Passionate about learning&innovation.🌐✨