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

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

[[382058]]

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

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

缺点:

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

 

责任编辑:武晓燕 来源: 狼王编程
相关推荐

2020-10-25 08:56:21

适配器模式

2013-11-26 16:39:21

Android设计模式

2012-05-16 17:22:11

Java设计模式

2022-02-13 23:33:24

设计模式Java

2012-04-12 09:33:02

JavaScript

2022-02-18 17:21:29

适配器模式客户端

2024-02-22 12:13:49

适配器模式代码

2021-08-16 17:15:19

设计模式Android适配器模式

2013-02-26 10:55:47

C#适配器设计模式

2024-04-10 12:27:43

Python设计模式开发

2022-12-12 09:20:59

适配器模式接口

2012-08-02 10:46:34

JavaAdapter模式

2009-11-18 18:08:20

PHP适配器模式

2022-05-29 22:55:00

适配器设计模式

2023-08-15 11:07:37

适配器模式TypeScript

2021-02-16 08:16:09

适配器模式MybatisJava

2014-07-17 10:55:10

Win8.1应用开发适配器模式

2023-09-06 13:20:00

适配器模式兼容性

2021-06-09 08:53:34

设计模式策略模式工厂模式

2012-01-13 15:59:07

点赞
收藏

51CTO技术栈公众号