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.
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.
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
(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:
Structure of object 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:
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:
Adapters that adopt a combination method are called object adaptersFeatures: Combine the "adapter" as an object into the adapter class to modify the target interface packaging To the adapterB, use inheritance to implement the adapter:Inherit the adapter:Powered by two-phase current through conversion
/** * 采用继承方式的插座适配器 */ public class TwoPlugAdapterExtends extends GBTowPlug implements ThreePlugIf{ @Override public void powerWithThree() { System.out.println("借助继承适配器"); this.powerWithTwo(); } }
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(); } }
By conversionThe inheritance method is called a class adapterFeatures:Through multiple inheritance of incompatible interfaces, the matching of the target interface is achieved, and the adaptation is realized for a single classPowered by two-phase current
With the help of inheritance adapterPowered by two-phase current
6. Application scenarios of the adapter pattern
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!