Design Patterns---Adapter Pattern

1 Introduction

In real life, it often happens that two objects cannot interact directly and a third-party object is required for adaptation. For example, a mobile phone and a SIM card need a card tray, so as to realize the reading of the SIM card. Another example is a mobile phone charger, which converts 220v AC voltage into 5v DC voltage, thereby providing charging power for mobile phones. This situation also occurs in software design, and we can use the adapter pattern to solve the problem.

2 Definitions 

Convert the interface of a class to another interface that the client wants, so that the two classes that are originally incompatible because of the interface can work together.

3 Structure and Implementation

The adapter pattern mainly includes the following three main roles:

1. Target: The interface that the current system can directly apply, which can be an interface or an abstract class.

2. Adaptee: A class that the system wants to access but cannot directly access.

3. Adapter: A converter that converts the adapter's interface into a target interface by inheriting or referencing the adapter's object, allowing clients to access the adapter in the format of the target interface.

The image below is from quote 2

4 Advantages and disadvantages

4.1 Advantages

1. The client can transparently call the target interface through the adapter.

2. Existing classes are reused. Programmers do not need to modify the original code and only need to add code to reuse the existing adapter class.

3. Decouple the target class and the adapter class to solve the problem of inconsistent interfaces between the target class and the adapter class.

4.2 Disadvantages

The adapter pattern itself is a disadvantage and should only be used as a last resort. The adapter writing process needs to be comprehensively considered in combination with business scenarios, which may increase the complexity of the system. Increase the difficulty of code reading, reduce code readability, and excessive use of adapters will make system code messy.

5 Code Examples

For an example of a power adapter, the power adapter outputs 220V AC voltage, which is converted to 5V DC voltage through the power adapter to charge the mobile phone.

5.1 Voltage class

@Data
@Builder
public class Voltage {

    /**
     * 电压类型 交流(AC) 直流(DC)
     */
    private String type;
    /**
     * 电压值
     */
    private int value;
}

5.2 PatchBoard

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-10-13 11:07
 * @desc: 插线板,设计好以后不允许修改
 **/
public final class PatchBoard {

    public Voltage giveVoltage(){
        return Voltage.builder()
                .type("AC")
                .value(220)
                .build();
    }
}

5.3 Target class Target

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-10-13 11:15
 **/
public interface Target {

    /**
     * 充电
     * @return 5v 直流电
     */
    Voltage charge();
}

5.4 Power Adapter PowerAdapter

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-10-13 11:15
 * @desc 充电器
 **/
public class PowerAdapter implements Target{

    private PatchBoard patchBoard;

    public PowerAdapter(PatchBoard patchBoard){
        this.patchBoard = patchBoard;
    }

    @Override
    public Voltage charge() {
        Voltage voltage = patchBoard.giveVoltage();
        //电压转换
        voltage.setType("DC");
        voltage.setValue(5);
        return voltage;
    }
}

5.5 Main function MainClass

/**
 * @program: design-pattern-learning
 * @author: zgr
 * @create: 2021-10-13 11:05
 **/
public class MainClass {

    public static void main(String[] args) {
        //想要给手机充电
        //1.找到插线板
        PatchBoard patchBoard = new PatchBoard();
        Voltage voltageAc = patchBoard.giveVoltage();
        System.out.println("插板给电:" + voltageAc);
        //2.拿出电源适配器
        Target target = new PowerAdapter(patchBoard);
        //3.充电
        Voltage voltageDc = target.charge();
        System.out.println("适配器给电:" + voltageDc);

    }
}

5.6 Running Results

5.7 Summary

 Our goal is to charge, the voltage given by the plug-in board cannot be used, and the adapter is used for conversion to meet the needs of the charging voltage.

6 Quotes

1. "Dahua Design Patterns"

2. Detailed explanation of adapter mode (Adapter mode)

7 Source code

design-pattern-learning/src/com/hz/design/pattern/adapter at main · airhonor/design-pattern-learning · GitHub

Guess you like

Origin blog.csdn.net/honor_zhang/article/details/120738884