To solve the problem

The so-called adapter mode is similar to the various ports of the computer, such as USB. As long as the terminal that implements THE USB protocol can be used on the computer, it only needs to provide the corresponding driver (Adaptor). The purpose of Adaptor is to shield the differences so that the core business is not affected by input and output.

It is mainly to solve the problem of code coupling, so that businesses can be well isolated; The core business code can be managed more centrally and can be invoked by multiple types of clients using adapters.

Application scenarios

Business isolation or a core business needs to be provided to many different types of clients. A well-known example is “hexagon architecture,” which makes good use of Adaptor to isolate input and output from confidence services.

It may also be a case back to the point where multiple core services need to adjust the business logic according to parameters so that the business logic is not visible to the outside world.

Schematic diagram

  • Target External interface
  • Adapter Adaptor that shields internal and external differences
  • Adaptee The role to which it is adapted

The sample

A common problem at work: We have a business, such as displaying user information. It requires both a presentation (HTTP/HTTPS) interface on the APP; The rest of the company also needs to use this information, so it needs to provide an interface internally (usually RPC, such as Thrift), and we use an adapter to solve this problem.

public interface UserInfo<T, R> {
    public T getUserInfo(R request);
}
public class UserCoreBussiness {

    public UserCoreResponse getUserInfo() {
        returnnew UserCoreResponse(); } public static class UserCoreRequest { } public static class UserCoreResponse { } } public class HttpAdaptor implements  UserInfo<String, HttpServletRequest> {@override public String getUserInfo(HttpServletRequest Request) {// HttpServletRequest is converted to the core request parameters this.buildRequest(request); UserCoreBussiness.UserCoreResponse response = new UserCoreBussiness().getUserInfo();return this.changeResponse(response);
    }

    private UserCoreBussiness.UserCoreRequest buildRequest(HttpServletRequest request) {
        return new UserCoreBussiness.UserCoreRequest();
    }

    private String changeResponse(UserCoreBussiness.UserCoreResponse response) {
        returnresponse.toString(); } } public class ThriftAdptor implements UserInfo<Object, Object> {@override public Object getUserInfo(Object Request) {// HttpServletRequest converts to core request parameters this.buildRequest(request); / / core business UserCoreBussiness UserCoreResponse response = new UserCoreBussiness () getUserInfo (); // Convert to the corresponding outputreturn this.changeResponse(response);
    }

    private UserCoreBussiness.UserCoreRequest buildRequest(Object request) {
        return new UserCoreBussiness.UserCoreRequest();
    }

    private Object changeResponse(UserCoreBussiness.UserCoreResponse response) {
        returnnew Object(); }}Copy the code

reference

https://en.wikipedia.org/wiki/Adapter_pattern