“This is the 10th day of my participation in the August More Text Challenge. For details, see: August More Text Challenge.”

The concept and role of the adapter pattern

(I) The concept of adapter

Adapters, also known as transformer patterns, are designed to change the interface of a class into another interface expected by the client, so that two classes that could not work together due to interface mismatch can work together. They are structural design patterns.

That is to say, the current system A and B, there are two kinds of interface customer support only visit to A interface, but the current system is not A interface object, but A B interface object, but the customer can’t identify B interface, so you need to pass an adapter C, converts B interface content to A interface, so that customers can get B interface from A interface to get content.

The adapter actually acts as a transformation/delegate, transforming one interface into another that meets the requirements.

(2) Roles of adapters

1. Target: The interface we expect

2. Source role (Adaptee) : an instance of an interface that exists in the system and meets the customer’s requirements (needs to be transformed), but the interface does not match

Adapter: Converts the source role into a class instance of the target role

Second, the adapter application pattern

1. An existing class whose methods do not match the requirements (methods with the same or similar results)

2. The adapter mode is not a design mode considered during the software design stage, but a solution in the case of similar functions but different interfaces caused by different products and different manufacturers during software maintenance.

Code examples for class adapters

Adaptee: AC220 class

public class AC220 {
    public int outputAC220V(a) {
        int output = 220;
        System.out.println(Output voltage: + output + "V");
        returnoutput; }}Copy the code

Target: indicates a DC5 interface

public interface DC5 {
    int outputDC5V(a);
}
Copy the code

Adapter: PowerAdapter class

public class PowerAdapter extends AC220 implements DC5 {
    @Override
    public int outputDC5V(a) {
        int adapterInput = super.outputAC220V();
        int adapterOutput = adapterInput / 44;
        System.out.println("Input AC" + adapterInput + "Output DC" + adapterOutput);
        returnadapterOutput; }}Copy the code

The test class:

public class Test {
    public static void main(String[] args) {
        DC5 adapter = newPowerAdapter(); adapter.outputDC5V(); }}Copy the code

Code examples for object adapters

Adaptee: AC220 class

public class AC220 {
    public int outputAC220V(a) {
        int output = 220;
        System.out.println(Output voltage: + output + "V");
        returnoutput; }}Copy the code

Target: indicates a DC5 interface

public interface DC5 {
    int outputDC5V(a);
}
Copy the code

Adapter: PowerAdapter class

public class PowerAdapter implements DC5 {

    private AC220 ac220;

    public PowerAdapter(AC220 ac220) {
        this.ac220 = ac220;
    }

    @Override
    public int outputDC5V(a) {
        int adapterInput = ac220.outputAC220V();
        int adapterOutput = adapterInput / 44;
        System.out.println("Input AC" + adapterInput + "Output DC" + adapterOutput);
        returnadapterOutput; }}Copy the code

The test class:

public class Test {
    public static void main(String[] args) {
        DC5 adapter = new PowerAdapter(newAC220()); adapter.outputDC5V(); }}Copy the code

Code examples for interface adapters

Adaptee: AC220 class

public class AC220 {
    public int outputAC220V(a) {
        int output = 220;
        System.out.println(Output voltage: + output + "V");
        returnoutput; }}Copy the code

Target: indicates the DC interface

public interface DC {

    int output5V(a);
    int output12V(a);
    int output24V(a);
    int output36V(a);

}
Copy the code

Adapter: PowerAdapter class

public class PowerAdapter implements DC {

    private AC220 ac220;

    public PowerAdapter(AC220 ac220) {
        this.ac220 = ac220;
    }

    @Override
    public int output5V(a) {
        int adapterInput = ac220.outputAC220V();
        int adapterOutput = adapterInput / 44;
        System.out.println("Input AC" + adapterInput + "Output DC" + adapterOutput);
        return adapterOutput;
    }

    @Override
    public int output12V(a) {
        return 0;
    }

    @Override
    public int output24V(a) {
        return 0;
    }

    @Override
    public int output36V(a) {
        return 0; }}Copy the code

The test class:

public class Test {
    public static void main(String[] args) {
        DC adapter = new PowerAdapter(newAC220()); adapter.output5V(); }}Copy the code

Comparison of adapter and decorator patterns

1, in the form of

(1) Decorator pattern: is a very special adapter pattern

(2) Adapter pattern: no hierarchy, decorators have hierarchy

2, definitions,

(1) Decorator pattern: The decorator and the decorator both implement the same interface. The main purpose is to preserve OOP relationships after extension

(2) Adapter pattern: wrapper in the form of inheritance or proxy

3, relationship,

(1) Decorator mode: satisfy the is-A relationship

(2) Adapter pattern: satisfies the has-a relationship

4, functionality,

(1) Decorator mode: focus on cover and extension

(2) Adapter mode: pay attention to compatibility and conversion

5, design

(1) Decorator mode: pre-consideration

(2) Adapter mode: after consideration

Advantages and disadvantages of the adapter pattern

(I) Advantages

1, can improve the class transparency and reuse, existing classes reuse but do not need to change

2. Decouple the target class and the adapter class to improve the extensibility of the program

3. It conforms to the open closed principle in many business scenarios

(ii) Shortcomings

1. The adapter writing process needs comprehensive consideration, which may increase the complexity of the system

2, increase the difficulty of code reading, reduce the readability of code, excessive use of adapters will make the system code become messy