Home >Java >javaTutorial >How to understand and use the Java adapter pattern?

How to understand and use the Java adapter pattern?

PHPz
PHPzforward
2023-04-23 09:19:141448browse

1. What is the adapter mode?

In fact, everyone should be familiar with the three words adapter. We know that Apple’s iPhone no longer provides a charging plug. There is only one data cable in the box, so we cannot use this data cable alone. It charges on a 220V plug-in board, so we need to buy a new adapter to connect the plug-in board and the mobile phone. This is the adapter.

In fact, it may also occur in software design that the components with certain business functions that need to be developed already exist in the existing component library, but they are incompatible with the interface specifications of the current system. If these components are re-developed, The cost of components is very high, so the adapter mode can solve these problems well.

2. Definition of adapter pattern

Adapter pattern: Convert the interface of a class into another interface that the customer wants, so that classes that cannot work together due to incompatible interfaces can Work.

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

3. Advantages and Disadvantages of the Adapter Mode

Advantages:

  • The client can transparently call the target interface through the adapter

  • Reuses existing classes. Programmers do not need to modify the original code and reuse existing adapter classes.

  • Unsolve the target class and adapter class Coupling solves the problem of inconsistent interfaces between target classes and adapter classes

  • Conforms to the opening and closing principle in many business scenarios

Disadvantages:

  • The adapter writing process needs to be fully considered based on the business scenario, which may increase the complexity of the system

  • Increases the difficulty of code reading and reduces the readability of the code sex, excessive use of adapters will make the system code messy

4. The structure and implementation of the adapter pattern

(The following structure is referenced from the Internet)

Structure of the pattern:

  • Target interface: The interface expected by the current system business, it can be an abstract class or interface

  • Adaptee class: It is the component interface in the existing component library that is accessed and adapted

  • Adapter class: It is a converter, through inheritance Or reference the adapter object, convert the adapter interface into the target interface, and let the client access the adapter in the format of the target interface

The structure of the class adapter pattern:

How to understand and use the Java adapter pattern?

Structure of object adapter pattern:

How to understand and use the Java adapter pattern?

5. Code implementation of adapter pattern

A. Use combination Way to implement adapter:

We now have a three-phase laptop plug, but we now need to use a two-phase plug for charging, so we need to install a two-phase adapter for this plug:

How to understand and use the Java adapter pattern?

Three-phase socket interface:

/**
 * 三相插座接口
 */
public interface ThreePlugIf {

    //使用三相电流供电
    public void powerWithThree();

}

National standard two-phase socket:

/**
 * 国标的二相插座
 */
public class GBTowPlug {
    public void powerWithTwo(){
        System.out.println("使用二相电流供电");
    }
}

Notebook:

/**
 * 笔记本
 */
public class NoteBook {

    private ThreePlugIf plug;

    public NoteBook(ThreePlugIf plug){
        this.plug = plug;
    }

    //使用插座充电
    public void charge(){
        plug.powerWithThree();
    }
}

Three Phase-to-two-phase socket adapter:

/**
 * 三相转二相的插座适配器
 */

public class TwoPlugAdapter implements ThreePlugIf {


    private GBTowPlug plug;

    public TwoPlugAdapter(GBTowPlug plug) {
        this.plug = plug;
    }

    @Override
    public void powerWithThree() {
        System.out.println("通过转换");
        plug.powerWithTwo();
    }
}

Test category:

public class Test {

    public static void main(String[] args) {

    GBTowPlug two = new GBTowPlug();
    ThreePlugIf three = new TwoPlugAdapter(two);
    NoteBook nb = new NoteBook(three);
    nb.charge();
	}
}

Output result:

Powered by two-phase current through conversion

Adapters that adopt a combination method are called object adapters

Features:

Combine the "adapter" as an object into the adapter class to modify the target interface packaging To the adapter

B, use inheritance to implement the adapter:

How to understand and use the Java adapter pattern?

Inherit the adapter:

/**
 * 采用继承方式的插座适配器
 */
public class TwoPlugAdapterExtends extends GBTowPlug implements ThreePlugIf{
    @Override
    public void powerWithThree() {
        System.out.println("借助继承适配器");
        this.powerWithTwo();
    }
}

Test class:

public class Test {

    public static void main(String[] args) {

    GBTowPlug two = new GBTowPlug();
    ThreePlugIf three = new TwoPlugAdapter(two);
    NoteBook nb = new NoteBook(three);
    nb.charge();

    //使用继承方法
    three = new TwoPlugAdapterExtends();
    nb = new NoteBook(three);
    nb.charge();
}
}

Output:

By conversion

Powered by two-phase current

With the help of inheritance adapter

Powered by two-phase current

The inheritance method is called a class adapter

Features:

Through multiple inheritance of incompatible interfaces, the matching of the target interface is achieved, and the adaptation is realized for a single class

6. Application scenarios of the adapter pattern

  • The previously developed system has classes that meet the functional requirements of the new system, but its interface is inconsistent with the interface of the new system

  • Use components provided by third parties, but the component interface definition is different from the interface definition required by yourself

The above is the detailed content of How to understand and use the 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