Adapter mode

Posted by Bike Racer on Sun, 13 Feb 2022 15:19:49 +0100

I. Introduction of models

1.1 definition

Convert the interface of a class into another interface that the customer wants. So that those classes that cannot work together due to incompatible interfaces can work together.

The adapter mode is divided into class structure mode and object structure mode. The former has a higher degree of coupling between classes than the latter, and requires programmers to understand the internal structure of relevant components in the existing component library, so there are relatively few applications.

1.2 advantages

  1. The client can call the target interface transparently through the adapter
  2. Reusing the existing classes, programmers do not need to modify the original code and reuse the existing adapter classes
  3. Decoupling the target class from the adapter class solves the problem of inconsistent interfaces between the target class and the adapter class
  4. Comply with the opening and closing principle

1.3 disadvantages

  1. The adapter writing process needs to be comprehensively considered in combination with the business scenario, which may increase the complexity of the system
  2. Increase the difficulty of code reading and reduce the readability of code. Excessive use of adapters will make the system code messy

2, Structure and Implementation

2.1 structure

  1. Target interface: the client calls the method of the adapter through the target interface and sends a request to the adapter. It can be an interface or an abstract class
  2. Adapter class: it is the desired interface to be accessed and adapted
  3. Adapter class: it is a converter that converts the adapter interface into the target interface by inheriting or referring to the adapter object, so that customers can access the adapter according to the specification of the target interface

Structure diagram of class adapter pattern

Structure diagram of object adapter pattern

2.2 realization

2.2.1,Adaptee

package com.erlang.adapter;

/**
 * @description: Adapter
 * @author: erlang
 * @since: 2022-02-13 21:56
 */
public class Adaptee {
    /**
     * Method of adapter
     */
    public void specificRequest() {
        System.out.println("The method in the adapter!");
    }
}

2.2.2,Target

package com.erlang.adapter;

/**
 * @description: Target object
 * @author: erlang
 * @since: 2022-02-13 21:55
 */
public interface Target {

    /**
     * Interface method
     */
    void request();
}

2.2.3 class adapter mode

ClassAdapter

package com.erlang.adapter;

/**
 * @description: Class Adapter 
 * @author: erlang
 * @since: 2022-02-13 21:56
 */
public class ClassAdapter extends Adaptee implements Target {
    @Override
    public void request() {
        System.out.println("Class adapter!");
        specificRequest();
    }
}

ClassAdapterClient

package com.erlang.adapter;

/**
 * @description: Class adapter method client test
 * @author: erlang
 * @since: 2022-02-13 22:03
 */
public class ClassAdapterClient {
    public static void main(String[] args) {
        Target target = new ClassAdapter();
        target.request();
    }
}

results of enforcement

Class adapter!
The method in the adapter!

2.2.4 object adapter mode

ObjectAdapter

package com.erlang.adapter;

/**
 * @description: object adapter 
 * @author: erlang
 * @since: 2022-02-13 21:59
 */
public class ObjectAdapter implements Target{
    private Adaptee adaptee;

    public ObjectAdapter(Adaptee adaptee) {
        this.adaptee = adaptee;
    }

    @Override
    public void request() {
        System.out.println("Object adapter!");
        adaptee.specificRequest();
    }
}

ObjectAdapterClient

package com.erlang.adapter;

/**
 * @description: Object adapter method client test
 * @author: erlang
 * @since: 2022-02-13 22:03
 */
public class ObjectAdapterClient {
    public static void main(String[] args) {
        Adaptee adaptee = new Adaptee();
        Target target = new ObjectAdapter(adaptee);
        target.request();
    }
}

results of enforcement

Object adapter!
The method in the adapter!

Topics: Design Pattern