Adapter
The Adapter pattern allows us to access the functionality of an object using a different interface. As the name suggests, it adapts an object so that it can be used by components expecting a different interface. The following diagram clarifies the situation:
The preceding diagram shows 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 the Adaptee. This pattern is pretty straightforward, so let's immediately work on an example.
Using LevelUP through the filesystem API
We are now going to build an Adapter around the LevelUP API, transforming it into an interface that is compatible with the core fs
module. In particular, we will make sure that every call to...