Search⌘ K
AI Features

The Adapter Pattern

Explore the Adapter pattern in Node.js structural design patterns. Learn how to create adapter modules that convert one interface to another, enabling compatibility between different APIs. This lesson guides you through implementing an adapter to use LevelUP with the filesystem API, enhancing your ability to integrate diverse modules.

We'll cover the following...

The Adapter pattern allows us to access the functionality of an object using a different interface.

A real-life example of an adapter would be a device that allows us to plug a USB Type-A cable into a USB Type-C port. In a generic sense, an adapter converts an object with a given interface so that it can be used in a context where a different interface is expected.

In software, the Adapter pattern is used to take the interface of an object (the adaptee) and make it compatible with another interface that’s expected by a given client. Let’s take a look at the illustration below to clarify this idea.

Adapter pattern schematic
Adapter pattern schematic

In the above illustration, we can see how the adapter is essentially a wrapper for the adaptee, exposing a different interface. The diagram also highlights the fact that the operations of the adapter can also be a composition of one or more method invocations on the adaptee. From an implementation perspective, the most common technique is composition, where the methods of the adapter ...