Adapter
The Adapter design pattern aims to convert one interface into another, much like the physical world examples of electrical plug adapters or USB adapters we encounter in daily life.
Imagine finding yourself in a hotel room late at night, with only 7% battery left on your phone. Your phone charger is in your office at the other end of the city, and all you have is an EU plug charger with a Mini USB cable. However, your phone requires a USB-C connection since you had to upgrade. To add to the challenge, the outlets in New York are USB-A. In this desperate situation, you urgently search for a Mini USB-to-USB-C adapter and hope you also brought your EU-to-US plug adapter. Time is running out as your battery reaches a mere 5%.
Just as adapters help us in the physical world, we can apply the same principle in code to enable compatibility between different interfaces.
Let’s explore how we can achieve this through interfaces. We start with the USPlug
interface, which...