This article mainly introduces the Java adapter mode. The editor thinks it is quite good. Now I will share it with you and give you a reference. Let’s follow the editor and take a look.
1. Concept
The adapter pattern converts one interface into another that the customer wants interface. It allows classes to work together that originally could not work together due to incompatible interfaces.
2. UML
3. More vivid Example
#4. Example analysis
package com.bjpowernode.adapter; /** * PS2接口,圆口 * @author eason * */ public interface PS2Port { public void workWithPS2(); }USBPort
##
package com.bjpowernode.adapter; /** * USB接口,U口 * @author eason * */ public interface USBPort { public void workWithUSB(); }
package com.bjpowernode.adapter; /** * 对象适配器 * 将PS2接口装换成USB接口 * 所以此类类型是USB接口(implements USBPort) + 成员变量ps2Port * @author eason * */ public class PS2ToUSB implements USBPort{ private PS2Port ps2Port; public PS2ToUSB(PS2Port ps2Port) { this.ps2Port = ps2Port; } @Override public void workWithUSB() { System.out.println("转换的关键在这里,本来是"); ps2Port.workWithPS2(); System.out.println("经过你的转换,现在是USB工作中"); } }
package com.bjpowernode.adapter; /** * 测试类 * client * @author eason * */ public class TestAdapter { public static void main(String[] args) { //我现在有一个PS2接口 PS2Port ps2Port = new PS2Port() { @Override public void workWithPS2() { System.out.println("PS2工作中"); } }; //但是我需要的是一个USB接口啊,对我(client)来说,我只认识USB接口 //经过PS2ToUSB的转换,PS2接口变成了USB接口 USBPort ps2ToUsbPort = new PS2ToUSB(ps2Port); ps2ToUsbPort.workWithUSB(); } }
#The above adapter is the object adapter. Let’s look at class adapters again.
PS2ToUSB, just simulate it briefly. Because Java does not allow multiple inheritance, there is no class pattern code in Java, only ideas.
package com.bjpowernode.adapter; /** * 类适配器 * @author eason * */ public class PS2ToUSB implements USBPort, PS2Port{ //重写workWithUSB,把工作交给workWithPS2 @Override public void workWithUSB() { workWithPS2(); } }
The difference is: the object adapter implements the interface (USB) that the client wants, and has a reference to the adapted object (PS2) internally. Adaptation function is achieved through combination. The class adapter implements the interface (USB) desired by the client and the adapted object interface (PS2), and implements the adaptation function through inheritance.
1. I hope to reuse some existing classes, but the interface is It is inconsistent with the requirements of the reuse environment.
2. In fact, the adapter mode is a bit helpless. In the early design, we should not consider the adapter mode, but should consider refactoring the unified interface.
They can both be used to wrap objects, the essential difference is
1. Adapter mode: Convert one interface to another interface.
2. Decorator pattern: Do not change the interface, only add responsibilities.
The above is the detailed content of Detailed explanation of graphic code about adapter pattern in Java. For more information, please follow other related articles on the PHP Chinese website!