Adapter Design Pattern

What is Adapter Design Pattern?

  • You’d have come across the Adapter many times in your life. 
  • Adapter Design Pattern acts as a bridge between two incompatible interfaces. 
  • It falls under Structural Design Pattern

Table of Contents

Real Life Examples of Adapter Design Pattern

Example 1:

usb photo

Let’s suppose if you’re using Mac book pro, then you’ll observe that it won’t be having USB-A ports at all. It has only USB-C ports. If you want to connect the USB port with your Macbook, you need to buy an adapter for that. It will convert the USB-A to the USB-C port.

Example 2:

memory card reader photoThe next example is a memory card reader. There won’t be an inbuilt memory card reader in most of the PCs. So, you need an external memory card reader which acts as an adapter.

Example 3:

phone charger photo

For charging your phone, you need to connect your phone with a phone charging adapter. In India, 220 V is the power output for household purposes. So, the phone charging adapter will cover the 220 V into 9 V for phone applications.

Example 4:

database photo

Now we’ll see a technical example for Adapter Design Pattern. When you are writing any Java application and that Java application needs to connect to the database. People who write Java applications are not aware of the databases behind it. It may be “Oracle”, “MySQL” or “Sybase”. If a new vendor is providing a better database, then the database behind that would change. So, we aren’t aware of the database running behind. We need to write an application, using some commands and statements. he adapter class would handle those problems. Let’s suppose if we are trying to migrate from “Oracle” to “Sybase”, then the adapter class will be changed. So this would be handled by this adapter class.

UML Class Diagram of Adapter Design Pattern

Adapter Design Pattern Java Implementation

  • In India, people get 240V electricity by default.
  • Mobile battery requires only 3V for charging the battery.
  • Here, the Mobile charger is acting as an adapter to convert 240V to 3V.
  • We are implementing an adapter class “SocketAdapterImpl” while would have various methods to get the required voltage. 

Step 1: Create a concrete class "Voltage" which has the field "voltage

public class Voltage
{

    private int voltage;

    public Voltage(int v)
    {
        this.voltage = v;
    }

    public int getVolts()
    {
        return voltage;
    }

    public void setVolts(int voltage)
    {
        this.voltage = voltage;
    }

}

Step 2: Create an interface "SocketAdapter" which would have get240Voltage(), get12Voltage(), and get3VVoltage() abstract methods

public interface SocketAdapter
{
     public Voltage get240Voltage();

     public Voltage get12Voltage();

     public Voltage get3VVoltage();
}

Step 3: Create a concrete class "Socket" which would return the default voltage. In India, the default voltage is 240Ve

public class Socket
{

    public Voltage getVoltage()
    {
        return new Voltage(240); //In India 240 is the default voltage
    }
}

Step 4: Create a SocketAdapterImpl concrete class that extends Socket and implements SocketAdapter interface"

public class SocketAdapterImpl extends Socket implements SocketAdapter
{

    //Using Composition for adapter pattern
    private Socket sock = new Socket();

    private Voltage convertVolt(Voltage v, int i)
    {
        return new Voltage(v.getVolts() / i);
    }

    @Override
    public Voltage get240Voltage()
    {
        return sock.getVoltage();
    }

    @Override
    public Voltage get12Voltage()
    {
        Voltage v = sock.getVoltage();
        return convertVolt(v, 20);
    }

    @Override
    public Voltage get3VVoltage()
    {
        Voltage v = sock.getVoltage();
        return convertVolt(v, 80);
    }
}

Step 5: Create a demo class "AdapterEx" while would use the Adapter class object for getting 3V and 12V

public class AdapterEx
{

    public static void main(String[] args)
    {
        SocketAdapter socketAdapter = new SocketAdapterImpl();
        Voltage voltage12 = socketAdapter.get12Voltage();
        System.out.println(voltage12.getVolts());
        
        Voltage voltage3 = socketAdapter.get3VVoltage();
        System.out.println(voltage3.getVolts());
        
        Voltage voltage240 = socketAdapter.get240Voltage();
        System.out.println(voltage240.getVolts());
    }
}

Step 6: Create a CivilEngineer class which uses HouseBuilder to build a House

public class CivilEngineer
{
    private HouseBuilder houseBuilder;
 
    public CivilEngineer(HouseBuilder houseBuilder)
    {
        this.houseBuilder = houseBuilder;
    }
 
    public House getHouse()
    {
        return this.houseBuilder.getHouse();
    }
 
    public void constructHouse()
    {
        this.houseBuilder.buildBasement();
        this.houseBuilder.buildStructure();
        this.houseBuilder.bulidRoof();
    }
}

Tutorial Videos of Adapter Design Pattern

Play Video
Play Video