Moe's Tech Blog

[Design Pattern] Adapter Pattern 본문

Software Design Pattern/Notes

[Design Pattern] Adapter Pattern

moe12825 2022. 8. 15. 01:44

Introduction

  • Thereis a common issue when the pre-existing system needs to incorprate third-party libraries or needs to connect to other systems
  • Adapter design pattern
    • Are frequently used because the output of one system may not conform to the expected input of another system
    • Helps to facilitate communication between two existing systems by providing a compatible interface

Adapter Pattern

  • Is composed of several parts:
    • 1. Client Class
      • Is a part of your system that wants to use a third-party library or external systems
    • 2. Adaptee Class
      • is the third-party library or external system to be used
    • 3. Adapter
      • sits between the client class and the adaptee
      • implements a target interface which is the interface that the client will use
  • How it works:
    • a. The client sents a request to the adapter using the target interface
    • b. The adapter translates the request into a message that the adaptee will understand
    • c. Once the translation is finished, it will send the translated request to the adaptee

 

Example

  • 1. Preexisting WebClient that we want to use to talk to another WebService
    • A WebClient expects to send any object into request but the service only supports a Json Object
    • An adapter needs to be used to convert from object request into JSON object.

 

How to Construct Adapter Pattern (based on above example)

  • Step 1: Design the target interface
    • The interface is what your adapter class will be implementing for your client class to use
public interface WebRequester {
    public int request(Object);
}
  • Step 2: Implement the target interface with the adapter class
public class WebAdapter implements WebRequester {
    private WebService service;

    public void connect(WebService currentService) {
        this.service = currentService
    }

    public int request(Object request) {
        Json result = this.toJson(request);
        Json response = service.request(result);
        if (response != null) {
            return 200;
        }
        return 500;
    }

    private Json toJson(object input) {...}
}

 

  • Step 3: Send the request from the client to the adapter using the target interface
public class WebClient {
    private WebRequester webRequester;

    public WebClient (WebRequester webRequester) {
        this.webRequester = webRequester;
    }

    private Object makeObject() {....} 

    public void doWork() {
        Object object = makeObject();
        int status = webRequester.request(object);

        if (status == 200) {
            System.out.println("OK");
        } else {
            System.out.println("Not OK");
        }
    }
}
  • Step 4: In the main program, instantiate the 
    • 1. Adapter (WebAdapter)
    • 2. Adaptee (WebService)
      • Is hidden from the client by the wrapping adapter class
    • 3. Client (WebClient)
public class Program {
    public static void main (String args[]) {
        String webHost = "Host: https://google.com/n/r";
        Webservice service = new WebService(webHost);
        WebAdapter adapter = new WebAdapter();
        adapter.connect(service);
        WebClient client = new WebClient(adapter);
        client.doWork();
    }
}

 

Note

  • Remember that an adapter is meant to:
    • Wrap the adaptee and expose 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