设计模式_结构型模式 -《适配器模式》

适配器模式用于将不同接口的类进行兼容,通过创建一个适配器类,转换不同接口之间的通信。文章介绍了类适配器和对象适配器两种模式,并通过读卡器的例子进行说明,展示了如何通过适配器使得电脑能读取不同类型的卡片数据。类适配器模式通过继承适配者类实现,而对象适配器则通过聚合适配者对象来实现,更符合合成复用原则。
摘要由CSDN通过智能技术生成

设计模式_结构型模式 -《适配器模式》

笔记整理自 黑马程序员Java设计模式详解, 23种Java设计模式(图解+框架源码分析+实战)

概述

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

image-20230110124752914

定义

  • 将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。
  • 适配器模式分为类适配器模式对象适配器模式,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些。

结构

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

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

类适配器模式

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

【例】读卡器

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

类图如下:

TFCard 和 TFCardImpl 是适配者类,SDCard 和 SDCardImpl 是目标接口,SDAdapterTF 就是适配器类。

我们让适配器类 SDAdapterTF 实现 SDCard 接口,并继承适配者类 TFCardImpl,这样就可以兼容 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 world SD";
            return msg;
        }
    
        public void writeSD(String msg) {
            System.out.println("sd card write msg : " + msg);
        }
    }
    
  • 电脑类

    public class Computer {
        
        // 从SD卡中读取数据
        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 world 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();
            // 读取SD卡中的数据
            String msg = computer.readSD(new SDCardImpl());
            System.out.println(msg);
    
            System.out.println("===============");
            // 使用该电脑读取TF卡中的数据
            // 创建适配器类对象
            SDAdapterTF sdAdapterTF = new SDAdapterTF(new TFCardImpl());
            String msg1 = computer.readSD(sdAdapterTF);
            System.out.println(msg1);
        }
    }
    

    输出

    SDCard read msg : hello world SD
    ===============
    adapter read tf card
    TFCard read msg : hello world TFcard
    

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

对象适配器模式

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

FutureTask 中的构造方法就使用了对象适配器模式,将 Runnable 转换为了 Callable;
具体可参考:FutureTask源码解析的构造方法

【例】读卡器

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

类图如下:

TFCard 和 TFCardImpl 是适配者类,SDCard 和 SDCardImpl 是目标接口,SDAdapterTF 就是适配器类。

我们让适配器类 SDAdapterTF 实现 SDCard 接口,但此时我们聚合适配者类 TFCard,而不是直接继承了。这样就满足了合成复用原则。如果没有接口规范 SDCard,我们可以直接继承具体的目标实现类 SDCardImpl,把上面类适配器模式的两个缺点全部解决了。

类适配器模式的代码,我们只需要修改适配器类(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));
        }
    }
    

    输出

    SDCard read msg : hello world SD
    ===============
    adapter read tf card
    TFCard read msg : hello world TFcard
    

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

应用场景

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

JDK源码解析-InputStreamReader

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 类的同名方法的调用封装。

类结构图如下:

image-20230107144804733

Reader、InputStream、StreamDecoder 这 3 个类用的就是标准的对象适配器模式,StreamDecoder 聚合了 InputStream,并且他还提供了两个 read 方法,其实这两个方法是重写父类 Reader 的方法,所以 StreamDecoder 就是一个适配器类,而它的父类 抽象类 Reader 就是目标接口,InputStream 就是适配者类。

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

结论:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小成同学_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值