企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
## **1. 基本介绍** >1.适配器模式(Adapter Pattern)将某个类的接口转换成客户端期望的另一个接口表示,主要的目的是兼容性,让原本因接口不匹配不能一起工作的两个类可以协同工作。其别名为包装器(Wrapper); 2.适配器模式属于结构型模式; 3.主要分为三类:类适配器模式,对象适配器模式,接口适配器模式。 ## **2. 工作原理** >1.适配器模式:将一个类的接口转换成另一种接口,让原本接口不兼容的类可以兼容; 2.从用户的角度看不到被适配者,是解耦合的; 3.用户调用适配器转化出来的目标接口方法,适配器再调用被适配者的相关接口方法 4.用户收到反馈结果,感觉只是和目标接口交互。 ![](https://img.kancloud.cn/03/60/03608274b1d2380bfe55d003af04d742_675x242.png) ## **3. 类适配器** #### 基本介绍 >Adapter类,通过继承src类,实现dst类接口,完成src->dst的适配 #### 原理图 >电脑3.0接口兼容键盘2.0接口 ![](https://img.kancloud.cn/4f/98/4f988d9733cee50ec8278753fd16c611_654x291.png) 代码实现 `电脑类:` ``` /** * 电脑 */ public class Computer { /** * 电脑串口为3.0 * @return */ public boolean usb3(){ System.out.println("电脑的USB接口为3.0"); System.out.println("该电脑串口正常运行"); return true; } } ``` `转换接口:` ``` /** * 兼容interface */ public interface Compatible { boolean usb2ExchangeUSB3(); } ``` `适配器类:` ``` /** * 适配器类 */ public class CompatibleAdapter extends Computer implements Compatible { /** * 3.0接口兼容2.0接口 */ @Override public boolean usb2ExchangeUSB3() { //获取电脑的3.0接口 boolean value = super.usb3(); if (value){ //TODO 接口3.0兼容2.0接口 System.out.println("接口3.0兼容2.0接口"); return true; } return false; } } ``` `键盘接入类:` ``` /** * 键盘接入 */ public class Keyboard { /** * 键盘USB为2.0 * @return */ public boolean access(Compatible compatible){ if (compatible.usb2ExchangeUSB3()){ System.out.println("电脑,键盘正常运行"); return true; } return false; } } ``` `测试类:` ``` public class Client { public static void main(String[] args) { System.out.println("类适配器模式"); Keyboard keyboard = new Keyboard(); boolean value = keyboard.access(new CompatibleAdapter()); if (value){ System.out.println("正常3.0兼容2.0"); }else{ System.out.println("不兼容"); } } } ``` `执行结果:` ``` 对象适配器模式 电脑的USB接口为3.0 该电脑串口正常运行 接口3.0兼容2.0接口 电脑,键盘正常运行 正常3.0兼容2.0 ``` `注意事项` >1.Java是单继承机制,所以类适配器需要继承src类,这一点算是一个缺点,因为这要求dst必须是接口,有一定的局限性; 2.src类的方法在Adapter中都会暴露出来,也增加了使用的成本; 3.由于其继承了src类,所以它可以根据需求重写src类的方法,使得Adapter的灵活性增强了。 ## **4. 对象适配器** #### 基本介绍 >1.基本思路和类的适配器模式相同,只是将Adapter类作修改,不是继承src类,而是持有src类的实例,以解决兼容性的问题。即:持有src类,实现dst类接口,完成src->dst的适配; 2.根据"合成复用原则",在系统中尽量使用关联关系来替代继承关系,因此大部分结构型模式都是对象结构型模式; 3.对象适配器模式是适配器模式常用的一种。 #### 原理图 >电脑3.0接口兼容键盘2.0接口 ![](https://img.kancloud.cn/c3/7a/c37a2516a4d0686a5a02dfc5d39d04f8_672x271.png) 代码实现 `电脑类:` ``` /** * 电脑 */ public class Computer { /** * 电脑串口为3.0 * @return */ public boolean usb3(){ System.out.println("电脑的USB接口为3.0"); System.out.println("该电脑串口正常运行"); return true; } } ``` `转换接口:` ``` /** * 兼容interface */ public interface Compatible { boolean usb2ExchangeUSB3(); } ``` `适配器类:` ``` /** * 适配器类 */ public class CompatibleAdapter implements Compatible{ //使用聚合关系,符合合成复用原则 private Computer computer; //构造方法 public CompatibleAdapter(Computer computer){ this.computer = computer; } /** * 3.0接口兼容2.0接口 */ @Override public boolean usb2ExchangeUSB3() { //获取电脑的3.0接口 boolean value = computer.usb3(); if (value){ //TODO 接口3.0兼容2.0接口 System.out.println("接口3.0兼容2.0接口"); return true; } return false; } } ``` `键盘类:` ``` /** * 键盘接入 */ public class Keyboard { /** * 键盘USB为2.0 * @return */ public boolean access(Compatible compatible){ if (compatible.usb2ExchangeUSB3()){ System.out.println("电脑,键盘正常运行"); return true; } return false; } } ``` `测试类:` ``` /** * 测试类 */ public class Client { public static void main(String[] args) { System.out.println("对象适配器模式"); Keyboard keyboard = new Keyboard(); boolean value = keyboard.access(new CompatibleAdapter(new Computer())); if (value){ System.out.println("正常3.0兼容2.0"); }else{ System.out.println("不兼容"); } } } ``` `执行结果:` ``` 对象适配器模式 电脑的USB接口为3.0 该电脑串口正常运行 接口3.0兼容2.0接口 电脑,键盘正常运行 正常3.0兼容2.0 ``` #### 注意事项 >1.对象适配器和类适配器其实算是同一种思想,只不过实现方式不同。根据合成复用原则,使用聚合替代继承,所以它解决了类适配器必须继承src的局限性问题,也不再要求dst必须是接口。 2.使用成本更低,更灵活。 ## **5. 接口适配器** #### 基本介绍 >1.当不需要 全部实现接口提供的方法时,设计一个抽象类实现接口,并为该接口中每个方法提供一个默认实现(空方法),那么该抽象类的子类可有选择地覆盖父类的某些方法来实现需求。 2.适用于一个接口不想使用其所有的方法的情况。 #### 原理图 ![](https://img.kancloud.cn/29/b9/29b9cc16f92c935c9c30a283452a6462_667x335.png) 代码实现 `电脑接口类:` ``` /** * 电脑接口 */ public interface ComputerInterface { //电源接口 void powerInterface(); //USB接口 void usbInterface(); //串行接口 void serialInterface(); //音频接口 void audioInterface(); } ``` `电脑抽象类适配器类:` ``` /** * 电脑抽象类适配器 */ public abstract class ComputerAdapter implements ComputerInterface{ //需要重写接口的全部方法,空实现(默认实现) @Override public void powerInterface() { } @Override public void usbInterface() { } @Override public void serialInterface() { } @Override public void audioInterface() { } } ``` `测试类:` ``` /** * 测试类 */ public class Client { public static void main(String[] args) { //匿名内部类 ComputerAdapter为抽象类 ComputerAdapter computerAdapter = new ComputerAdapter(){ //如果业务仅需要覆盖powerInterface方法,不需要重写其他方法 @Override public void powerInterface() { System.out.println("使用接口适配器模式,仅调用powerInterface方法"); } }; computerAdapter.powerInterface(); } } ``` `执行结果:` ``` 使用接口适配器模式,仅调用powerInterface方法 ```