结构型模式 - 适配器模式(Adapter Pattern)

简介: 结构型模式 - 适配器模式(Adapter Pattern)

开启掘金成长之旅!这是我参与「掘金日新计划 · 12 月更文挑战」的第7天,点击查看活动详情

前言

一、 适配器模式概述

如果去欧洲国家去旅游的话,他们的插座如下图最左边,是欧洲标准。而我们使用的插头如下图最右边的。因此我们的笔记本电脑,手机在当地不能直接充电。所以就需要一个插座转换器,转换器第1面插入当地的插座,第2面供我们充电,这样使得我们的插头在当地能使用。生活中这样的例子很多,手机充电器(将220v转换为5v的电压),读卡器等,其实就是使用到了适配器模式。

网络异常,图片无法展示
|

适配器模式定义: 将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。

适配器模式分为类适配器模式对象适配器模式,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些。

二、适配器模式结构

适配器模式(Adapter)包含以下主要角色:

  • 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
  • 适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口。
  • 适配器(Adapter)类:它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。

三、类适配器模式

实现方式:定义一个适配器类来实现当前系统的业务接口,同时又继承现有组件库中已经存在的组件。

【例】读卡器 现有一台电脑只能读取SD卡,而要读取TF卡中的内容的话就需要使用到适配器模式。创建一个读卡器,将TF卡中的内容读取出来。

类图如下:

网络异常,图片无法展示
|

代码如下:

//SD卡的接口

public interface SDCard {  
   //读取SD卡方法  
   String readSD();  
   //写入SD卡功能  
   void writeSD(String msg);  
}  
//SD卡实现类  
public class SDCardImpl implements SDCard {  
   public String readSD() {  
       String msg = "sd card read a msg :hello word SD";  
       return msg;  
  }  
   public void writeSD(String msg) {  
       System.out.println("sd card write msg : " + msg);  
  }  
}
//电脑类  
public class Computer {  
   public String readSD(SDCard sdCard) {  
       if(sdCard == null) {  
           throw new NullPointerException("sd card null");  
      }  
       return sdCard.readSD();  
  }  
} 
//TF卡接口  
public interface TFCard {  
   //读取TF卡方法  
   String readTF();  
   //写入TF卡功能  
   void writeTF(String msg);  
}
//TF卡实现类  
public class TFCardImpl implements TFCard {  
   public String readTF() {  
       String msg ="tf card read msg : hello word tf card";  
       return msg;  
  }  
   public void writeTF(String msg) {  
       System.out.println("tf card write a msg : " + msg);  
  }  
}
//定义适配器类(SD兼容TF)  
public class SDAdapterTF extends TFCardImpl implements SDCard {  
   public String readSD() {  
       System.out.println("adapter read tf card ");  
       return readTF();  
  }  
   public void writeSD(String msg) {  
       System.out.println("adapter write tf card");  
       writeTF(msg);  
  }  
}
//测试类  
public class Client {  
   public static void main(String[] args) {  
       Computer computer = new Computer();  
       SDCard sdCard = new SDCardImpl();  
       System.out.println(computer.readSD(sdCard));  
       System.out.println("------------");  
       SDAdapterTF adapter = new SDAdapterTF();  
       System.out.println(computer.readSD(adapter));  
  }  
}

类适配器模式违背了合成复用原则。类适配器是客户类有一个接口规范的情况下可用,反之不可用。

四、对象适配器模式

实现方式:对象适配器模式可釆用将现有组件库中已经实现的组件引入适配器类中,该类同时实现当前系统的业务接口。

【例】读卡器

我们使用对象适配器模式将读卡器的案例进行改写。类图如下:

网络异常,图片无法展示
|

代码如下:

类适配器模式的代码,我们只需要修改适配器类(SDAdapterTF)和测试类。

//创建适配器对象(SD兼容TF)

public class SDAdapterTF  implements SDCard {  
   private TFCard tfCard;  
   public SDAdapterTF(TFCard tfCard) {  
       this.tfCard = tfCard;  
  }  
   public String readSD() {  
       System.out.println("adapter read tf card ");  
       return tfCard.readTF();  
  }  
   public void writeSD(String msg) {  
       System.out.println("adapter write tf card");  
       tfCard.writeTF(msg);  
  }  
}
//测试类  
public class Client {  
   public static void main(String[] args) {  
       Computer computer = new Computer();  
       SDCard sdCard = new SDCardImpl();  
       System.out.println(computer.readSD(sdCard));  
       System.out.println("------------");  
       TFCard tfCard = new TFCardImpl();  
       SDAdapterTF adapter = new SDAdapterTF(tfCard);  
       System.out.println(computer.readSD(adapter));  
  }  
}

注意: 还有一个适配器模式是接口适配器模式。当不希望实现一个接口中所有的方法时,可以创建一个抽象类Adapter ,实现所有方法。而此时我们只需要继承该抽象类即可。

五、 适配器模式应用场景

  • 以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致。
  • 使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同。

六、JDK源码解析

Reader(字符流)、InputStream(字节流)的适配使用的是InputStreamReader。

InputStreamReader继承自java.io包中的Reader,对他中的抽象的未实现的方法给出实现。如:

public int read() throws IOException {  
   return sd.read();  
}  
public int read(char cbuf[], int offset, int length) throws IOException {  
   return sd.read(cbuf, offset, length);  
}

如上代码中的sd(StreamDecoder类对象),在Sun的JDK实现中,实际的方法实现是对sun.nio.cs.StreamDecoder类的同名方法的调用封装。类结构图如下:

网络异常,图片无法展示
|
从上图可以看出:

  • InputStreamReader是对同样实现了Reader的StreamDecoder的封装。
  • StreamDecoder不是Java SE API中的内容,是Sun JDK给出的自身实现。但我们知道他们对构造方法中的字节流类(InputStream)进行封装,并通过该类进行了字节流和字符流之间的解码转换。

结论: 从表层来看,InputStreamReader做了InputStream字节流类到Reader字符流之间的转换。而从如上Sun JDK中的实现类关系结构中可以看出,是StreamDecoder的设计实现在实际上采用了适配器模式。

后记

喜欢我的文章的朋友点点喜欢、收藏,也欢迎朋友们评论区留下你的意见和建议,恕毅在此拜谢!


相关文章
|
7月前
|
设计模式
设计模式8 - 适配器模式【Adapter Pattern】
设计模式8 - 适配器模式【Adapter Pattern】
25 0
|
3月前
适配器模式(Adapter Pattern)
适配器模式(Adapter Pattern)
|
12月前
|
设计模式 Java 数据库连接
Java设计模式-适配器模式(Adapter)
Java设计模式-适配器模式(Adapter)
|
应用服务中间件 智能硬件 容器
结构型模式 - 外观模式(Facade Pattern)
结构型模式 - 外观模式(Facade Pattern)
结构型模式 - 装饰器模式(Decorator Pattern)
结构型模式 - 装饰器模式(Decorator Pattern)
|
存储 设计模式 安全
结构型模式 - 组合模式(Composite Pattern)
结构型模式 - 组合模式(Composite Pattern)
|
设计模式 Java 程序员
适配器模式(Adapter Pattern)
适配器模式是一种结构型设计模式, 它能将接口转换为客户期望的另一个接口,也就是说它能使接口不兼容的对象能够相互合作。
102 0
适配器模式(Adapter Pattern)
|
设计模式
结构型-Adapter
适配器模式的原理与实现 适配器模式 的英文翻译是 Adapter Design Pattern。顾名思义,这个模式就是用来做适配的,它将不兼容的接口转换为可兼容的接口,让原本由于接口不兼容而不能一起工作的类可以一起工作。对于这个模式,有一个经常被拿来解释它的例子,就是 USB 转接头充当适配器,把两种不兼容的接口,通过转接变得可以一起工作。 原理很简单,我们再来看下它的代码实现。适配器模式有两种实现方式:类适配器和对象适配器。其中,类适配器使用继承关系来实现,对象适配器使用组合关系来实现。具体的代码实现如下所示。其中,ITarget 表示要转化成的接口定义。Adaptee 是一组不兼容 ITa
94 0
结构型-Adapter
|
设计模式 Java
【Java设计模式系列】装饰器模式(Decorator Pattern)(上)
【Java设计模式系列】装饰器模式(Decorator Pattern)
120 0
【Java设计模式系列】装饰器模式(Decorator Pattern)(上)
|
设计模式 C#
【愚公系列】2021年12月 二十三种设计模式(六)-适配器模式(Adapter Pattern)
【愚公系列】2021年12月 二十三种设计模式(六)-适配器模式(Adapter Pattern)
104 0
【愚公系列】2021年12月 二十三种设计模式(六)-适配器模式(Adapter Pattern)