Adapter Design Pattern in Java

Amit Kumar
3 min readMar 27, 2022

Definition of Adapter Pattern

Let’s start by giving a definition of adapter pattern:

Adapter design pattern is one of the structural design pattern and its used so that two unrelated interfaces can work together. The object that joins these unrelated interface is called an Adapter.

An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected directly. An Adapter wraps an existing class with a new interface so that it becomes compatible with the client’s interface.

The main motive behind using this pattern is to convert an existing interface into another interface that the client expects. It’s usually implemented once the application is designed.

One of the great real life example of Adapter design pattern is mobile charger. Mobile battery needs 3 volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket.

Adapter Pattern Example

Consider a scenario in which there is an app that’s developed in the US which returns the top speed of luxury cars in miles per hour (MPH). Now we need to use the same app for our client in the UK that wants the same results but in kilometers per hour (KMPH).

To deal with this problem, we’ll create an adapter which will convert the values and give us the desired results:

First, we’ll create the original interface Movable which is supposed to return the speed of some luxury cars in miles per hour:

public interface Movable {
// returns speed in MPH
double getSpeed();
}

We’ll now create one concrete implementation of this interface:

public class Bmw implements Movable {

@Override
public double getSpeed() {
return 268;
}
}

Now we’ll create an adapter interface MovableAdapter that will be based on the same Movable class. It may be slightly modified to yield different results in different scenarios:

public interface MovableAdapter {
// returns speed in KMPH
double getSpeed();
}

The implementation of this interface will consist of private method convertMPHtoKMPH() that will be used for the conversion:

public class MovableAdapterImpl implements MovableAdapter {
private Movable luxuryCars;

// standard constructors
@Override
public double getSpeed() {
return convertMPHtoKMPH(luxuryCars.getSpeed());
}

private double convertMPHtoKMPH(double mph) {
return mph * 1.60934;
}
}

Now we’ll only use the methods defined in our Adapter, and we’ll get the converted speeds. In this case, the following assertion will be true:

@Test
public void whenConvertingMPHToKMPH_thenSuccessfullyConverted() {
Movable bmw = new BMW();
MovableAdapter bmwAdapter = new MovableAdapterImpl(bmw);

assertEquals(bmwAdapter.getSpeed(), 431.30312, 0.00001);
}

As we can notice here, our adapter converts 268 mph to 431 km/h for this particular case.

When to Use Adapter Pattern

  • When an outside component provides captivating functionality that we’d like to reuse, but it’s incompatible with our current application. A suitable Adapter can be developed to make them compatible with each other
  • When our application is not compatible with the interface that our client is expecting
  • When we want to reuse legacy code in our application without making any modification in the original code

Advantages

  • It allows two or more previously incompatible objects to interact.
  • It allows reusability of existing functionality and flexibility.
  • Client class is not complicated by having to use a different interface and can use polymorphism to swap between different implementations of adapters.

Disadvantages

  • All requests are forwarded, so there is a slight increase in the overhead.
  • Sometimes many adaptations are required along an adapter chain to reach the type which is required.

Adapter Design Pattern Example in JDK

Some of the adapter design pattern example I could easily find in JDK classes are;

  • java.util.Arrays#asList()
  • java.io.InputStreamReader(InputStream) (returns a Reader)
  • java.io.OutputStreamWriter(OutputStream) (returns a Writer)

That’s all for adapter design pattern in java.

Happy Learning!

Thank you for reading

Feel free to share any questions or suggestions in the comment section!

--

--