• 【设计模式】—适配器设计模式


    1.概述
    如下图所示,为了使得三孔的插座能够被常规的两个接线柱的插头使用,得需要使用一个交流电适配器来进行转换。

    又比如手机充电器能将220V得电压转换为5V适合手机充电‘、读卡器等等生活中常见得就是使用到了适配器模式。

    定义:

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

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

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

    目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
    适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口。
    适配器(Adapter)类:它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。
    3.类适配器模式
    实现方式:定义一个适配器类来实现当前系统的业务接口,同时又继承现有组件库中已经存在的组件。

    【例】读卡器

    现有一台电脑只能读取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));
    }
    }

    分析

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

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

    【例】读卡器

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


    代码如下:

    类适配器模式的代码,我们只需要修改适配器类(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 ,实现所有方法。而此时我们只需要继承该抽象类即可。

    5.应用场景
    以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致。
    使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同。
    6.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的设计实现在实际上采用了适配器模式。

  • 相关阅读:
    activity(工作流)初步学习记录
    npm install出现npm ERR! write after end解决方法
    npm ERR! Cannot read property 'match' of undefined 错误处理
    gist.github.com 被墙无法访问解决办法
    AS SSD Benchmark固态硬盘检测工具
    VSCode 云同步扩展设置 Settings Sync 插件
    window系统不显示预览图片的处理方法
    VsCode如何同时打开多个项目
    win10的开机启动文件夹在哪及开机自动启动软件
    Java环境配置和tomcat环境配置
  • 原文地址:https://www.cnblogs.com/zzsuje/p/16265733.html
Copyright © 2020-2023  润新知