《2022年适配器模式 .pdf》由会员分享,可在线阅读,更多相关《2022年适配器模式 .pdf(6页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、适配器 : 基于现有类所提供的服务,向客户提供接口,以满足客户的期望Java 设计模式类适配器客户的开发人员定义了一个接口,期望用这个接口来完成整数的求和操作,接口定义如下:Java 代码1. public interface Operation 2. public int add(int a,int b); 3. 开发人员在了解这个接口的定义后,发现一个第三方类, 里面有一个方法能实现他们期望的功能,其代码如下:Java 代码1. public class OtherOperation 2. public int otherAdd(int a,int b) 3. return a + b;
2、4. 5. 以上第三方类 OtherOperation的方法 public int otherAdd(int a,int b)所提供的功能,完全能符合客户的期望,所以只需要想办法把OtherOperation的otherAdd(int a,int b)和客户的 Operation 接口联系起来,让这个第三方类来为客户提供他们期望的服务就行了,这样就避免了开发人员再度去研究类似OtherOperation的 otherAdd(int a,int b)方法的实现(利用已有的轮子,避免重复发明),这方法之一,就是用适配器模式:Java 代码1. public class AdapterOperati
3、on extends OtherOperation implements Operation 2. public int add(int a,int b) 3. return otherAdd(a,b); 4. 5. 以上就是适配器的实现方法之一,类适配器 ,在以上实现中存在着三中角色分别是:名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 1 页,共 6 页 - - - - - - - - - 1:适配目标角色: Operation 。2:适配类(原)角色: OtherOperati
4、on 。3:适配器角色: AdapterOperation 。其中适配器角色是适配器模式的核心。适配器的主要工作就是通过封装现有的功能,使他满足需要的接口。对象适配器我们再来看看另一种情况:假如客户接口期望的功能不止一个,而是多个:Java 代码1. public interface Operation 2. public int add(int a,int b); 3. public int minus(int a,int b); 4. public int multiplied(int a,int b); 5. 而能提供这些实现的原可能不止一个:Java 代码1. public class
5、 OtherAdd 2. public int otherAdd(int a,int b) 3. return a + b; 4. 5. 6.7. public class OtherMinus 8. public int minus(int a,int b) 9. return a - b; 10. 11. 12. 13.public class OtherMultiplied 14. public int multiplied(int a,int b) 15. return a * b; 16. 17. 由于 java 是不能实现多继承的,所以我们不能通过构建一个适配器,让他来继承所有原以
6、完成我们的期望,这时候怎么办呢?只能用适配器的另一种实现- 对象适配器 :Java 代码名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 2 页,共 6 页 - - - - - - - - - 1. public class AdapterOperation implements Operation 2. private OtherAdd add; 3. private OtherMinus minus; 4. private OtherMultiplied multiplied; 5
7、.6. public void setAdd(OtherAdd add) 7. this.add = add; 8. 9.10. public void setMinus(OtherMinus minus) 11. this.minus = minus; 12. 13. 14. public void setMultiplied(OtherMultiplied multiplied) 15. this.multiplied = multiplied; 16. 17. 18. /适配加法运算19. public int add(int a,int b) 20. return add.otherA
8、dd(a,b); 21. 22. 23. /适配减法运算24. public int minus(int a,int b) 25. return minus.minus(a,b); 26. 27. 28. /适配乘法运算29. public int multiplied(int a,int b) 30. return multiplied.multiplied(a,b); 31. 32. 上面代码很明显,适配器并不是通过继承来获取适配类(原)的功能的,而是通过适配类 的对象来获取的,这就解决了java 不能多继承所带来的不便了。这也是 java 提倡的编程思想之一,即 尽量使用聚合不要使用继承
9、。还有一种情况是需要使用对象适配器 的。我们来看看,单我们的客户提供的需求并不是一个明确的接口,而是一个类,并没有定义期望的方法,如下Java 代码1. public class A 2. public int add(int a,int b) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 3 页,共 6 页 - - - - - - - - - 3. return a + b; 4. 5. 现在客户要一个新类B,要求能在保留类 A功能的情况下增加一个运算减法的功能,并要求 B能随时
10、替换掉 A但不能对已有系统造成影响。 这样我们只能新建一个类 B,并让 B继承 A。Java 代码1. public class B extends A 2. b() 3. super(); 4. 5.6. public int minus(int a,int b) 7. /待实现的减法运算函数 . 8. 9. 这时候,我们发现类C已经提供了实现减法的函数,Java 代码1. public class C 2. public int minus(int a,int b) 3. return a - b; 4. 5. 为了避免重复去设计该函数, 我们决定引入 C类,通过适配 C类来达到我们的期望
11、,但问题是 A和 C都是一个具体类,我们无法让B同时继承这个两个类,而B继承 A又是必须的, 所以我们只能考虑把C给内聚到 B内部,对象适配器 又得派上用场了。Java 代码1. public class B extends A 2.3. private C c; 4.5. B() 6. super(); 7. 8.名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 4 页,共 6 页 - - - - - - - - - 9. public void setMinus(C c) 10.
12、this.c= c; 11. 12. 13. public int minus(int a,int b) 14. return c.minus(a,b); 15. 16. 这样,在需要 A 类的地方都能用 B类来代替,同时又保证了新的功能的引入。更灵活的实现 - 隐藏目标接口的抽象适配器做 java 桌面应用的都知道WindowListener 接口,Java 代码1. public interface WindowListener extends EventListener 2. public void windowActivated(WindowEvent e);3. public voi
13、d windowClosed(WindowEvent e);4. public void windowClosing(WindowEvent e);5. public void windowDeactivated(WindowEvent e);6. public void windowDeiconified(WindowEvent e);7. public void windowIconified(WindowEvent e);8. public void windowOpened(WindowEvent e);9. 要实现这个接口, 我们就必须实现它所定义的所有方法,但是实际上, 我们很少需
14、要同时用到所有的方法, 我们要的只是其中的两三个。 为了不使我们实现多余的方法,jdk WindowListener提供了一个 WindowListener 的默认实现类 WindowAdapter类,这是一个抽象类,Java 代码1. public abstract class WindowAdapter implements WindowListener 2. public void windowActivated(WindowEvent e) 3. public void windowClosed(WindowEvent e) 4. public void windowClosing(W
15、indowEvent e) 5. public void windowDeactivated(WindowEvent e) 6. public void windowDeiconified(WindowEvent e) 7. public void windowIconified(WindowEvent e) 8. public void windowOpened(WindowEvent e) 名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 5 页,共 6 页 - - - - - - - - - 9. WindowAdapter类对 WindowListener 接口的所有有方法都提供了空实现,有了 WindowAdapter 类,我们只需要去继承WindowAdapter,然后选择我们所关心的方法来实现就行了,这样就避免了直接去实现WindowListener 接口。名师资料总结 - - -精品资料欢迎下载 - - - - - - - - - - - - - - - - - - 名师精心整理 - - - - - - - 第 6 页,共 6 页 - - - - - - - - -
限制150内