Home  >  Article  >  Java  >  What are the implementation methods and application scenarios of Java adapter pattern?

What are the implementation methods and application scenarios of Java adapter pattern?

王林
王林forward
2023-05-19 09:07:051709browse

Introduction

The adapter pattern in Java is a structural design pattern that converts the interface of a class into the interface expected by another client. The adapter pattern allows incompatible classes to be used together Work, it does this by converting incompatible interfaces into compatible interfaces.

The adapter pattern contains the following three roles:

  • Target interface (Target): Definition The interface expected by the client is the interface that the client needs to use.

  • Source interface (Adaptee): an existing, incompatible interface, that is, the interface that needs to be adapted .

  • Adapter (Adapter): A class that converts the source interface into the target interface. It implements the target interface and holds a reference to the source interface to forward client requests to the source. Interface.

In the adapter pattern, the adapter acts as an intermediate layer that converts the client's request into a format acceptable to the target class. The adapter is usually implemented in the following two ways:

  • Class adapter pattern: In the class adapter pattern, the adapter inherits both the target class and the source class, and implements the target interface. This allows the adapter to convert the functions of the source class into the target class. Interface.

  • Object Adapter Pattern: In the object adapter pattern, the adapter holds an instance of the source class and implements the target interface. This allows the adapter to convert the functionality of the source class into the target Class interface.

Implementation

Suppose we have a power plug, but we want to plug it into a device that can only accept the USB interface. At this time We can use an adapter to achieve.

Target interface

public interface USB {
    /**
     * 充电
     */
    void charge();
}

Source interface

public class PowerSocket {
    /**
     * 提供电源
     */
    protected void powerSupply(){
        System.out.println("提供电源");
    }
}

Class adapter

public class PowerSocketToUSBAdapter extends PowerSocket implements USB {
    /**
     * 充电
     */
    @Override
    public void charge() {
        powerSupply();
        System.out.println("转换为USB充电...");
    }
}

Object adapter

public class PowerSocketToUSBAdapter1 implements USB{
    private PowerSocket powerSocket;
    public PowerSocketToUSBAdapter1(PowerSocket powerSocket) {
        this.powerSocket = powerSocket;
    }
    /**
     * 充电
     */
    @Override
    public void charge() {
        powerSocket.powerSupply();
        System.out.println("转换为USB充电...");
    }
}

Test

    public static void main(String[] args) {
        // 类适配器
        USB usb1 = new PowerSocketToUSBAdapter();
        usb1.charge();
        System.out.println();
        // 对象适配器
        USB usb2 = new PowerSocketToUSBAdapter1(new PowerSocket());
        usb2.charge();
    }
}

What are the implementation methods and application scenarios of Java adapter pattern?

In this example, PowerSocket is the source interface, which provides the power supply method; USB is the destination interface, which defines the charging method.

  • **Class adapter mode:**Inherit the PowerSocket class and implement the USB interface, convert PowerSocket to USB charging

  • **Object adapter mode:* * Pass the PowerSocket class into PowerSocketToUSBAdapter1 through the constructor, and call PowerSocket's powerSupply() method in the charge() method to provide power

** Class adapter and object adapter Difference: **Class adapter is inheritance between classes, object adapter is the composition relationship of objects, which can also be said to be the association relationship of classes. This is the fundamental difference between the two

Summary

In Java , the object adapter pattern is more commonly used than the class adapter pattern. This is because the object adapter pattern uses a combination relationship, which can more flexibly replace the adapted object, and will not affect the original inheritance relationship due to changes in the adapter. In addition, Object adapters also comply with the opening and closing principle, so when you need to add a new adapter, you only need to implement the adapter interface without modifying the original code. The class adapter mode requires the use of multiple inheritance. And Java does not support multiple inheritance, so in Java It is difficult to use the class adapter pattern in .

The adapter pattern is a very practical design pattern. It can help us adapt incompatible interfaces without changing the original code structure. Meet business needs.

Advantages

  • The adapter mode can make the compatibility between interfaces better and effectively reuse existing classes.

  • The adapter mode can decouple the client from the specific implementation, improving the flexibility and maintainability of the code.

  • The adapter mode can enhance the system's Scalability, you can easily expand new functions without affecting the stability of the system.

Disadvantages

  • Adapter mode requires adding an Adapter objects increase the complexity of the system.

  • The adapter pattern will affect the readability and understandability of the code, and you need to carefully consider how to name and design the interface of the adapter class.

Application Scenario

  • When you need to use an existing class, but its interface does not meet the requirements, you can use the adapter pattern to adapt Configuration.

  • When different classes need to be processed using the same method, you can use the adapter mode for adaptation.

  • When transparency is required When you use a subclass or extended class of a class, you can use the adapter pattern for adaptation.

  • When you need to integrate a class into multiple systems, you can use the adapter pattern to Ensure compatibility between systems.

The above is the detailed content of What are the implementation methods and application scenarios of Java adapter pattern?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete