Design Pattern – Adapter Pattern – Part VII

Till now, we have discussed all creational types of design patterns. In this post, we will be creating a demo about structural design patterns. In this series, our first design pattern is Adapter design pattern. As said, this design pattern is structural design pattern. This design pattern combines the capabilities of two independent interfaces. It basically acts like a bridge between two incompatible interfaces.

Easiest example to understand adapter pattern in real life is the electric socket in different continents provide different voltages. A traveler from Asia can use an adapter in Europe to get 240 V of electricity for electronic devices.

When to use Adapter Design Pattern?

Firstly, when a client expects different interface than available, at that time adapter pattern can help to convert a interface of a class into another interface that the client can use. However, Adapter pattern allows reuse of lot of code and it is one of the major reasons why it is most favorable among software engineering. Similarly, a real example you would find in JDK libraries of InputStreamReader and OutputStreamWriter.

How to use Adapter Design Pattern?

So in this implementation, we will show how to use Adapter design pattern. We have a traveler from Asia traveling in Europe, he wants to use his electronic device which needs 240 V of electricity from socket, but Europe only provides 120 V of electricity. We will design an adapter class that will convert 120 V of electricity to 240 V of electricity.

Our target class or client class is AsiaSocket, as shown below:


package com.betterjavacode.designpatterns.adapterexample;

public class AsiaSocket {

    public void provideElectricity() {
        System.out.println("Provide electricity of 240 V");
    }
}

It’s a simple class with a method provideElectricity.

Our adaptee class is EuropeSocket which implements an interface IEuropeSocket as shown below:


package com.betterjavacode.designpatterns.adapterexample;

public class EuropeSocket implements IEuropeSocket {

    public void getElectricity() {
        System.out.println("Get electricity of 120 V");
    }

}

Secondly, we will implement an Adapter class that will provide adapter between Europe and Asia Socket classes. This will look like below:


package com.betterjavacode.designpatterns.adapterexample;

public class EuropeAsiaAdapter implements IEuropeSocket {

    AsiaSocket asiaSocket;

    public EuropeAsiaAdapter(AsiaSocket asiaSocket) {
        this.asiaSocket = asiaSocket;
    }

    public void getElectricity() {
        asiaSocket.provideElectricity();
    }

}

This class has a constructor to instantiate AsiaSocket and implements IEuropeSocket interface.

Now in our demo class, we will show how to use this adapter pattern.


package com.betterjavacode.designpatterns;

import com.betterjavacode.designpatterns.adapterexample.AsiaSocket;
import com.betterjavacode.designpatterns.adapterexample.EuropeAsiaAdapter;
import com.betterjavacode.designpatterns.adapterexample.EuropeSocket;
import com.betterjavacode.designpatterns.adapterexample.IEuropeSocket;

public class AdapterDemo {

    public static void main(String[] args) {
        EuropeSocket es = new EuropeSocket();

        AsiaSocket as = new AsiaSocket();

        IEuropeSocket europeAsiaAdapter = new EuropeAsiaAdapter(as);

        System.out.println(" Electricity in Asia ");
        as.provideElectricity();

        System.out.println(" Electricity in Europe ");
        es.getElectricity();

        System.out.println(" Electricity for Asia in Europe");
        europeAsiaAdapter.getElectricity();

    }

}

Therefore, if you run this demo class, the output will show that we will be getting electricity of 240 V for Asian electronic devices in Europe.

Download

In conclusion, we showed how to use the Adapter pattern. The demo code will be available on GitHub repository here.

References

  1. Adapter Design Pattern
  2. Adapter Design Pattern in Java