...

/

The Adapter Pattern

The Adapter Pattern

Learn how to use the Adapter pattern to build an fs-like interface that writes to LevelDB instead.

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.

Press + to interact
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 provide a bridge to the methods of ...