Adapter Design Pattern: Plug Converter Demo

πŸ“˜ Scenario:

You're traveling through Europe, and you bring along your trusty US laptop and phone chargers. But when you arrive and go to plug them in, you hit a problem β€” Europe uses Type C plugs with two round pins, while your chargers have the typical US Type A plug with two flat pins. There's no way to connect them directly. Luckily, you have a plug adapter with you. This small but essential device acts as a translator, converting your charger's plug shape so it fits perfectly into the European socket. Just like magic, your charger works seamlessly β€” all thanks to this simple interface adapter that bridges two incompatible systems.

The adapter converts the interface (plug type) so the device (your charger) can work with the existing system (European socket)..

Problem:

Your US charger has a Type A plug (with flat pins), but European sockets accept only Type C plugs (with round pins). Without a direct match, you can’t connect your charger to the power supply β€” they are incompatible systems.

πŸ”Œ Adapter Pattern Solution:

Just like using a plug adapter, the Adapter Pattern bridges the gap between incompatible interfaces. It wraps your US charger (the client) in a compatible interface (Type C plug) so it can seamlessly connect with the European socket (the system). The underlying device remains unchanged β€” only the interface is adapted.

UML Diagram:

UML Diagram of Adapter Pattern

Output:

Phone starts charging...
Adapting plug...
Power supplied using US standard plug.
πŸ”— View Code on GitHub

You brought a US charger to Europe... ⚑ Let's plug it in using an adapter!