TailTemplate Build stunning websites faster with our pre-designed Tailwind CSS templates

Understanding Design Patterns - Adapter

converts the interface of a class into another interface the clients expect. Adapter lets class work together that couldn’t otherwise because of incompatible interfaces.

Today, there are 15 types of electrical outlet plugs in use worldwide. When you travel from China to Singapore, you might find yourself in trouble if you do not have a socket adapter. Marina, who was travelling to Singapore for a symposium, was very upset when she realized that she cannot charge her phone and gadgets in her hotel room because the plug and socket don't match. She went to the reception area and asked the hotel staff if they have any adapters. The hotel staff was quite courteous but they informed her that they don't and Marina would have to buy one from the local hardware.

In Singapore, the outlet plugs in use are type G, whereas electronic devices from China are using the type A plugs. The type G socket interface is not compatible with a type A plug, which expects a type A socket interface. In this case we need to use a type G to type A socket adapter.

This is an example of the Adapter Pattern. Let us see how it looks like in code.

First, we have a TypeAPlug class and a TypeGSocket class:

class TypeAPlug
{
    public function connect(TypeASocketInterface $socket)
    {
        $socket->connectTwoPins();
    }
}
 
class TypeGSocket
{
    public function connectThreePins() {}
}

As we can see, connect() method of TypeAPlug class expects a TypeASocketInterface type and it calls a method connectTwoPins() of TypeASocketInterface type. We need to create a type G socket interface to type A socket interface adapter. We will name it GtypeToAtypeSocketAdatper:

class GtypeToAtypeSocketAdatper implements TypeASocketInterface
{
    private $typeGSocket = null;
    public function __construct(TypeGSocket $typeGSocket)
    {
        $this->typeGSocket = $typeGSocket;
    }
 
    public function connectTwoPins()
    {
        // some algorithms to be used
        $this->connectThreePins();
    }
}

GtypeToAtypeSocketAdatper class implements TypeASocetInterface interface, and it accepts an TypeGSocket class when it is instantiated. The composition-favored technique used here is very similar to the one in the Decorator Pattern. The key difference is that the Decorator Pattern retains the class type of the class it wraps. But the Adapter Pattern changes the class type of the class it wraps to the one clients expects.

Finally, let us see how type G socket is adapter to type A socket and used by type A plug:

$typeAPlug = new TypeAPlug();
$typeGSocket = new TypeGSocket();
$gTypeToAtypeAdapter = new GtypeToAtypeSocketAdatper($typeGSocket);
$typeAPlug->connect($gTypeToAtypeAdapter);

In the Adapter Pattern, there are some components to help you get familiar with the pattern:

  • Adaptee: This is the class, that exists before the adapter class is introduced. In our case, it would be the TypeGSocket class.
  • Adapter: This is the class, that wraps an adaptee class and makes it compatible with what client class expects. This class decouples adaptee class from client class. In our case, it is the GtypeToAtypeSocketAdatper class.
  • Client: This is the class that makes requests to the Adapter and receives what it expects. It is not aware of existence of the adaptee class. In our case, it is the TypeAPlug class.

In our example, the Adapter Pattern converts the interface of a class (TypeGSockt class) into another interface (TypeAPlugInterface interface) the clients (TypeAPlug) expect. The Adapter(GtypeToAtypeSocketAdatper) allows class to work together what couldn’t otherwise work because of incompatible interfaces.

The end

Hopefully this simple tutorial helped you with your development. If you like our post, please follow us on Twitter and help spread the word. We need your support to continue. If you have questions or find our mistakes in above tutorial, do leave a comment below to let us know.