Adapter mode of design pattern 13

background

In real life, if I want to charge an Apple phone, but I only have an Android charging cable, here I also have an Android to Apple adapter cable. So I can charge the iPhone by using an Android cable and an adapter cable.

Then the adapter cable here acts as an adapter.

We will also encounter similar scenarios in the development. A method with a certain business function already exists, but it does not conform to the interface specification of the current system. If the method is re-developed, the cost will be high. At this time, we can use the adapter mode to solve this problem.

Schematic diagram

As shown on the left, if two components A and B want to be combined, C in the right of the picture is needed. This C is the adapter.

What is the adapter mode

Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Two classes that do not match and cannot work together can work together.)

The adapter mode mainly consists of the following elements:

  • Target interface: The interface expected by the current system business, which 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 that converts the adapter interface into a target interface by inheriting or referring to the object of the adapter, allowing the client to access the adapter in the format of the target interface.

The relationship diagram is as follows:

Adapter mode

Code

Target

Adaptee

ClassAdapter

Test code:

public static void main(String[] args){
    System.out.println("类适配器模式测试:");
    Target target = new ClassAdapter();
    target.request();
}

The test results are as follows:

类适配器模式测试:
适配者中的业务代码被调用!

You see, the adapter mode is not very simple. Just write an adapted class to inherit the target class and implement the interface that needs to be adapted.

Thoughts on the Adapter Mode

When we have ready-made components to meet business needs, but this component does not conform to the interface specifications of the new system. We can adapt the mode.

Adapter mode is best not to consider it in the detailed design stage. It is not to solve the problem that is still in the development stage, but to solve the problem of the project in service. No system analyst will consider using the adapter mode when doing detailed design. The main scenario used in this mode is to expand the application, the system is expanded, and the adapter mode is considered to reduce the risk of code modification when it does not conform to the original design.

Recommended in the past

Scan the QR code to get more exciting. Or search Lvshen_9 on WeChat , you can reply to get information in the background

1.回复"java" 获取java电子书;

2.回复"python"获取python电子书;

3.回复"算法"获取算法电子书;

4.回复"大数据"获取大数据电子书;

5.回复"spring"获取SpringBoot的学习视频。

6.回复"面试"获取一线大厂面试资料

7.回复"进阶之路"获取Java进阶之路的思维导图

8.回复"手册"获取阿里巴巴Java开发手册(嵩山终极版)

9.回复"总结"获取Java后端面试经验总结PDF版

10.回复"Redis"获取Redis命令手册,和Redis专项面试习题(PDF)

11.回复"并发导图"获取Java并发编程思维导图(xmind终极版)

Another: Click [ My Benefits ] to have more surprises.

 

Guess you like

Origin blog.csdn.net/wujialv/article/details/109350408