Design Pattern 06 Adapter Pattern

Adapter Pattern is a structural pattern

Overview

Structural patterns focus on how to organize existing classes or objects together to form a more powerful structure.

In life, we often encounter such a problem: thin and light notebooks usually only have type-c or usb-a interfaces and no network ports. However, in daily use, it is often necessary to connect to the network port to access the Internet. The first solution that comes to mind is to buy an adapter or docking station . They can convert type-c or usb-a into other types of interfaces for us to use. In fact, this is an adapter mode .

Why is our commonly used charging head called a power adapter ? The traditional power supply is 220V AC, but mobile phones may only require 5V for charging, so although there is power now, they cannot be charged directly. It is also impossible to ask the power company to provide us with a 5V DC power supply. At this time, the power adapter comes into play. For example, Apple's ancestral 5V1A charging head actually converts 220V AC power into 5V DC power for transmission. This is equivalent to playing the role of an adapter between 220V AC and mobile phones .

The adapter pattern is divided into class adapter and object adapter.

Code

Here is an example of a computer that requires an adapter to connect to a network cable to access the Internet:

class adapter

1. Define computer

/**
 * 电脑
 * <p>想上网,需要插网线
 */
public class Computer {
    
    

    /**
     * 电脑需要连接上转换器才可以上网
     *
     * @param adapter 转接器
     */
    public void connect(NetToUsb adapter) {
    
    
        
        // 上网的具体实现,找一个转接头
        adapter.connectNetLine();
    }
}

2. Define the network cable

/**
 * 网线
 */
public class NetLine {
    
    

    /**
     * 上网
     */
    public void online(){
    
    
        System.out.println("连接网线上网");
    }
}

3. Define usb to network port adapter

/**
 * 转接器
 */
public interface NetToUsb {
    
    

    /**
     * 处理请求,usb -> 网线
     */
    void connectNetLine();
}

4. Define class adapter

/**
 * 类适配器
 */
public class ClassAdapter extends NetLine implements NetToUsb {
    
    

    @Override
    public void connectNetLine() {
    
    
        // 可以上网了
        super.online();
    }
}

5. Use the adapter to connect to the network cable to access the Internet.

// 电脑
Computer computer = new Computer();
// 转接器已经插上网线
ClassAdapter adapter = new ClassAdapter();
// 电脑连接转接器,成功上网
computer.connect(adapter);

This implementation requires occupying an inheritance bit. If NetToUsb is not an interface but an abstract class at this time, it cannot be implemented because Java does not support multiple inheritance. At the same time, according to the principle of synthesis and reuse, functions should be implemented more through synthesis.

So let’s take a look at the second, which is also the more commonly used pattern: object adapter .

object adapter

4. Define object adapter

/**
 * 对象适配器
 */
public class ObjectAdapter implements NetToUsb {
    
    

    /**
     * 网线
     */
    private NetLine netLine;

    public ObjectAdapter(NetLine netLine) {
    
    
        this.netLine = netLine;
    }

    @Override
    public void connectNetLine() {
    
    
        // 可以上网了
        netLine.online();
    }
}

5. Use the adapter to connect to the network cable to access the Internet.

/**
 * 测试示例
 */
public class ObjectAdapterTest {
    
    

    @Test
    public void test() {
    
    
        // 电脑
        Computer computer = new Computer();
        // 网线
        NetLine netLine = new NetLine();
        // 转接器插上网线
        ObjectAdapter adapter = new ObjectAdapter(netLine);
        // 电脑连接转接器,成功上网
        computer.connect(adapter);
    }

}

This method does not occupy inheritance bits, meets the principle of composition and reuse, has lower coupling, and is more flexible, so it is recommended .

Advantages and Disadvantages

advantage

1. Any two unrelated classes can be run together.

2. Improved class reuse.

3. Increased the transparency of the class.

4. Good flexibility.

shortcoming

1. Excessive use of adapters will make the system very messy and difficult to grasp as a whole. For example, it is obvious that the A interface is being called, but in fact it is internally adapted to implement the B interface. If this happens too much in a system, it will be tantamount to a disaster. Therefore, if it is not necessary, you can reconstruct the system directly without using the adapter.

2. Since Java inherits at most one class, it can only adapt to at most one class, and the target class must be an abstract class.

scenes to be used

If you are motivated to modify the interface of a functioning system, you should consider using the Adapter pattern.

Precautions

Adapters are not added during detailed design, but rather to solve problems for the project in service.


reference

https://www.bilibili.com/video/BV1mc411h719?p=7&vd_source=299f4bc123b19e7d6f66fefd8f124a03

Guess you like

Origin blog.csdn.net/qq_37770674/article/details/132455710