A Complete Guide To Design Patterns: The Adapter Design Pattern

A Complete Guide To Design Patterns: The Adapter Design Pattern

The adapter design pattern is a structural design pattern that's very useful when maintaining a legacy code or adding a new features.


Baraa Abuzaid
Baraa Abuzaid
@baraaabuzaid
A Complete Guide To Design Patterns: The...

The adapter design pattern is a structural design pattern. As the name drove from the adapter in the physical world, the adapter design pattern work in a similar way. In many cases, our app needs to interact with other systems or libraries that we can’t change it or even we might need to maintain a legacy system that might break if it undergoes any changes.
In these circumstances the use of the adapter design pattern shine.
Furthermore, In the adapter design pattern, we have an adapter that is responsible to mediate the communication between an outside system or service (the Adaptee) and a client. Meanwhile, internally the adapter class wraps the adaptee and expose its functionality by adhering to an interface that the client is familiar with. So by doing that a client class will be able to use a new service or library (the adaptee) without any modifications.

To make things clear let’s take a look at the adapter pattern in UML class diagram. And if you ain’t familar with UML please check out my UML Class diagram tutorial

 

When to use the adapter design pattern

So consider a situation in which you have a web client class in your app trying to interact with the remote server. The endpoint only accepts a JSON. Now you should do some sort of conversion from a plain old java object (POJO) to JSON. At some point in time, the endpoint changed to only accept XML. if you are about to anticipate such a scenario in your app the perfect design pattern to use would be an adapter design pattern. Furthermore, we could have JSON Adapter, XML Adapter and we could use these adapters interchangeably without any need to modify our web client class.

Now we can illustrate the example in UML Class diagram as below.

 

 

IWebRequest Interface

public interface IWebRequest {
    int request(Object Data);
}

 

JsonRequestAdapter Class

public class JsonRequestAdapter implements IWebRequest {
    WebServices webServices;

    public JsonRequestAdapter(WebServices webServices) {
        this.webServices = webServices;
    }

    @Override
    public int request(Object data) {
        Json body = pojoToJson(data);
        Json response = webServices.postRequest(body);
        if(response != null){
            return 200; // success status code
        }

        return 500; //  Error status code
    }

    public void connect(String url){
       // connecting to the end point ...
    }

    public Json pojoToJson(Object data){

        // code for converting to json ...
        return null;
    }
}

 

WebClient Class

public class WebClient  {
    IWebRequest webRequest;

    public WebClient(IWebRequest webRequest) {
        this.webRequest = webRequest;
    }

    private Object createAttachment(String content){

        // create plan old java object ...

        return null;
    }
    public void upload(String content){
        Object data = createAttachment(content);
        int resultCode = webRequest.request(data);
        if (resultCode == 200){
            System.out.println("Status: Ok!");
        }else {
            System.out.println("Status: Error!");
        }
    }
}

Main Class

public class Main {
    public static final void main(String[] args){

        WebServices webServices = new WebServices();

        JsonRequestAdapter jsonRequestAdapter = new JsonRequestAdapter(webServices);
        jsonRequestAdapter.connect("https://baraabytes.com");

        WebClient webClient = new WebClient(jsonRequestAdapter);
        webClient.upload("User data");

    }
}

 

 

 

Photo By Steve Johnson

Show Comments (1)

Comments

  • Rupesh
    Rupesh

    Thanks alot for very nice explaination with real life example. I have found one more blog and would like to share for other readers, who want to read more. Adapter design pattern in java

    • Article Author
    • Reply

Related Articles

A Complete Guide To Design Patterns In Kotlin: Composite Design Pattern
Kotlin

A Complete Guide To Design Patterns In Kotlin: Composite Design Pattern

A composite design pattern is built upon two principles in object-oriented programming. First, decomposition by breaking a whole into parts. Then generalization which generalizing...

Posted on by Baraa Abuzaid
A Complete Guide To Design Patterns: Façade Design Pattern
Object Oriented Design

A Complete Guide To Design Patterns: Façade Design Pattern

  The Façade design pattern is a structural design pattern. It reduces coupling and improves code readability. you are very likely to use the Façade pattern when you would...

Posted on by Baraa Abuzaid