Structural Design Pattern: 2. Adapter Pattern

What is an adapter pattern?

Jhanak Didwania
TRICK THE INTERVIEWER

--

Welcome to the part 2 of structural design pattern. As we have previously seen in the Facade pattern, it deals with how to simplify the client interaction with a complex system. If you haven’t read about it yet, go check it out: https://medium.com/trick-the-interviwer/structural-design-pattern-1-facade-pattern-a11ff68f2c03

The adapter pattern is a metaphor for real life adapter. An adapter is a device that converts attributes of one electrical device or system to those of an otherwise incompatible device or system. Adapter takes the input plug and convert it into an output plug that a switch board can support.

The Adapter Design Pattern works in a similar manner. It is used to make two incompatible interfaces interact with each other by providing a compatible interface in between. It does not add any other additional functionality.

Let’s take an example to understand better.

Analogy

Suppose there is a housing price prediction service, that takes existing housing price data, that consist of the house area, the number of rooms and the price in rupees in a JSON format and predict the price for a new house with say x area and y number of rooms.

Now, there can be multiple clients who might require the prediction from this prediction service, as this service might be implementing a costly logic in it like a regression model to predict prices for new houses and has a very good precision and accuracy in it. So, in order to interact with this service, clients need to send the JSON request to this service with attributes as area and number of rooms and this service will produce the result.

It might be possible that, some clients don’t want to send JSON data or some might want to send data in some other format like XML. In such a case, the service will be incompatible with such clients. In order to overcome this scenario, adapter comes into picture. Similar to the real world adapter, client will not directly interact with the service and instead will interact with an adapter. The role of the adapter will be to take in the client requests and convert them to the service’s understandable format, and later call the service from the adapter itself.

Components of Adapter Pattern

  1. Client class
  2. Interface through which client interacts with the service
  3. Adapter class which makes client and service compatible with each other
  4. Adaptee/Service which finally process the request

Generalized UML Diagram

Image taken from internet

Steps to implement adapter pattern

  1. Interface: the target interface that your adapter class will be implementing for your client class to use
public interface HousePricePredictor {
public String predictHousePrice(XMLObject object);
}

2. Adapter: The adapter class provides the methods that will take the client class’s object and convert it into a JSON object. The adapter should convert any instance of a class that the client can create and send that in a request. The adapter class also transfers the translated request to the service. Dependency of the service is injected in the adapter class. The client class therefore only needs to know about the target interface of the adapter.

public class PredictionServiceAdapter implements HousePricePredictor {

private HousingPricePredictionService predictionService;

public PredictionServiceAdapter(HousingPricePredictionService predictionService) {
this.predictionService = predictionService;
}

@Override
public String predictHousePrice(XMLObject object) {
JSONObject jsonObject = parseJson(object);
return this.predictionService.predictHousePrice(jsonObject);
}
}

3. Service: The service that will run it’s logic to finally predict the house price and return the predicted price to the client.

public class HousingPricePredictionService {

public String predictHousePrice(JSONObject object){
//logic to predict price for the house
String predictedPrice = runRegressionModel(object);
return predictedPrice;
}

}

4. Client: The service is hidden from the client, it is the interface to which the client is actually interacting which is in-turn implemented by an adapter.

public class Client {
public static void main(String[] args) {
XMLObject object;
PredictionServiceAdapter adapter =
new PredictionServiceAdapter(new HousingPricePredictionService());
String price = adapter.predictHousePrice(object);
}
}

This is a very basic implementation of adapter pattern and it aims to give you a brief overview of the intent of this pattern. You can refer to the various sources on the internet to know more about this pattern.

Summary

  • Wrap the adaptee/service and exposes a target interface to the client.
  • Indirectly change the adaptee’s interface into one that the client is expecting by implementing a target interface.
  • Indirectly translate the client’s request into one that the adaptee is expecting.
  • Reuse an existing adaptee with an incompatible interface.

Well, do you wonder, why we didn’t change the implementation of the adaptee or the client itself to make them compatible?

  1. Changing Adaptee: It might be possible that multiple clients are using the same service and it’s not always feasible to overload the method according to the needs of every client. One more reason is, it is possible that adaptee is a third party library or an external system which we can’t change directly.
  2. Changing client code: Changing your system to match the other system is not always a solution either, because an update by the vendors to the outside systems may break part of our system.

Hence, it is not recommended to make a change in the client and the service request formats due to compatibility issues and instead we should introduce a layer of adapter in between them to deal with their compatibility issues. Next time, whenever there is change in the service, we can directly make that change in the adapter without the client knowing about it.

That’s all for the adapter pattern. Now, you might be appreciating the power of this pattern and it is widely used in the industry and it’s good to know about it. You may refer to the book, Gang of Four’s design pattern catalog for more understanding.

Please let me know in the comments below, if you have any doubts. If you like this blog, please clap and share it among your friends. Happy software engineering!

--

--