Adapter in JavaScript
The adapter pattern, similar to the other structural design patterns, focuses on interfaces.
In the adapter pattern’s case, it involves being able to use a new implementation without changing the consumer or the implementation’s interface. The “adapter” takes the new implementation and “adapts” the interface to match what the consumer expects.
We’re not changing the implementation or the consumer; rather, we’re building an adapter to wrap the implementation and plug it into the consumer without changing either.
Implementation
Let’s start with a simple in-memory database that uses a naive IdGenerator to generate keys for the database entries by encoding the object as a string.
Database has a createEntry method that stores given data using the IdGenerator to generate a key. Database also has a get method to recall entries by ID:
class IdGenerator {
get(entry) {
...