当前位置:主页 > 查看内容

设计模式系列之适配器模式

发布时间:2021-05-28 00:00| 位朋友查看

简介:本文转载自微信公众号「狼王编程」,作者狼王。转载本文请联系狼王编程公众号。 1、概述 适配器模式是一种结构型设计模式, 它能使接口不兼容的对象能够相互合作。 2、适用场景 1)当你希望使用某个类, 但是其接口与其他代码不兼容时, 可以使用适配器类。 2……

本文转载自微信公众号「狼王编程」,作者狼王。转载本文请联系狼王编程公众号。 

1、概述

适配器模式是一种结构型设计模式, 它能使接口不兼容的对象能够相互合作

2、适用场景

1)当你希望使用某个类, 但是其接口与其他代码不兼容时, 可以使用适配器类。

2)如果您需要使用这样一些类, 他们处于同一个继承体系, 并且他们又有了额外的一些共同的方法, 但是这些共同的方法不是所有在这一继承体系中的子类所具有的共性。可以将这些方法封装在一个装饰器中。

3、实例

有以下场景:

方钉适配圆孔的适配器,方钉想放到圆孔中,则圆孔的直径等于方钉的对角长度。

  1. 方钉适配圆孔的适配器,方钉想放到圆孔中,则圆孔的直径等于方钉的对角长度。 
  2.  
  3. 定义方钉、圆孔 
  4.  
  5. 圆孔: 
  6. 直径 
  7.  
  8. 圆钉: 
  9. 直径 
  10.  
  11. 方钉: 
  12. 边长 

定义方钉:

  1. public class SquareNails { 
  2.  
  3.     public double getWidth() { 
  4.         return width; 
  5.     } 
  6.  
  7.     public void setWidth(double width) { 
  8.         this.width = width; 
  9.     } 
  10.  
  11.     public SquareNails(double width) { 
  12.         this.width = width; 
  13.     } 
  14.  
  15.     /** 
  16.      * 边长 
  17.      */ 
  18.     private double width; 
  19.  

定义圆钉:

  1. public class RoundNails { 
  2.  
  3.     /** 
  4.      * 直径 
  5.      */ 
  6.     private double diameter; 
  7.  
  8.     public double getDiameter() { 
  9.         return diameter; 
  10.     } 
  11.  
  12.     public void setDiameter(double diameter) { 
  13.         this.diameter = diameter; 
  14.     } 
  15.  
  16.     public RoundNails(double diameter) { 
  17.         this.diameter = diameter; 
  18.     } 

定义圆孔:

  1. public class RoundHold { 
  2.  
  3.     /** 
  4.      * 直径 
  5.      */ 
  6.     private double diameter; 
  7.  
  8.     public RoundHold(double diameter) { 
  9.         this.diameter = diameter; 
  10.     } 
  11.  
  12.     public double getDiameter() { 
  13.         return diameter; 
  14.     } 
  15.  
  16.     public void setDiameter(double diameter) { 
  17.         this.diameter = diameter; 
  18.     } 
  19.  
  20.     /** 
  21.      * 校验是否合适 
  22.      * @param roundNails 
  23.      * @return 
  24.      */ 
  25.     public boolean fits(RoundNails roundNails){ 
  26.         if (diameter >= roundNails.getDiameter()){ 
  27.             return true
  28.         }else { 
  29.             return false
  30.         } 
  31.  
  32.     } 

定义适配器:

  1. public class SquareNailsRoundHoldAdapter { 
  2.  
  3.     public RoundNails getResult(SquareNails squareNails){ 
  4.         double width = squareNails.getWidth(); 
  5.         double diagonal = width * Math.sqrt(2); 
  6.         RoundNails roundNails = new RoundNails(diagonal); 
  7.         return roundNails; 
  8.     } 

测试类:

  1. @RunWith(SpringRunner.class) 
  2. @SpringBootTest(classes = TestApplication.class) 
  3. public class TestDemo { 
  4.  
  5.     @Test 
  6.     public void test() { 
  7.         //定义个圆孔 
  8.         RoundHold roundHold = new RoundHold(10); 
  9.         //定义圆钉 
  10.         RoundNails roundNails = new RoundNails(10); 
  11.         //定义方钉,边距10 
  12.         SquareNails squareNails10 = new SquareNails(10); 
  13.         //定义方钉,边距6 
  14.         SquareNails squareNails6 = new SquareNails(6); 
  15.         //适配器 
  16.         SquareNailsRoundHoldAdapter squareNailsRoundHoldAdapter = new SquareNailsRoundHoldAdapter(); 
  17.         RoundNails result10 = squareNailsRoundHoldAdapter.getResult(squareNails10); 
  18.         RoundNails result6 = squareNailsRoundHoldAdapter.getResult(squareNails6); 
  19.         //圆钉是否合适 
  20.         if (roundHold.fits(roundNails)) { 
  21.             System.out.println("this round nails is fits"); 
  22.         } else { 
  23.             System.out.println("this round nails is does not fits"); 
  24.         } 
  25.         //10方钉是否合适 
  26.         if (roundHold.fits(result10)) { 
  27.             System.out.println("squareNails10 is fits"); 
  28.         } else { 
  29.             System.out.println("squareNails10 is does not fits"); 
  30.         } 
  31.         //6方钉是否合适 
  32.         if (roundHold.fits(result6)) { 
  33.             System.out.println("squareNails6 is fits"); 
  34.         } else { 
  35.             System.out.println("squareNails6 is does not fits"); 
  36.         } 
  37.     } 

结果:

  1. this round nails is fits 
  2. squareNails10 is does not fits 
  3. squareNails6 is fits 

4、总结

优点:

1)单一原则:将代码或者数据转换的过程从主要业务逻辑区分出来。

2)开闭原则:只要客户端代码通过客户端接口与适配器进行交互, 你就能在不修改现有客户端代码的情况下在程序中添加新类型的适配器。

缺点:

增加代码复杂度。使用时需要考虑是否在原功能上修改更加简单。


本文转载自网络,原文链接:https://mp.weixin.qq.com/s/cTBky-Pgjpdjk9XwDwhk8Q
本站部分内容转载于网络,版权归原作者所有,转载之目的在于传播更多优秀技术内容,如有侵权请联系QQ/微信:153890879删除,谢谢!
上一篇:大白话聊访问者模式:从入门到实践 下一篇:没有了

推荐图文

  • 周排行
  • 月排行
  • 总排行

随机推荐