• 设计模式 - 结构型模式考点篇:适配器模式(类适配器、对象适配器、接口适配器)


    目录

    一、适配器模式

    一句话概括结构式模式

    1.1、适配器模式概述

    1.2、案例

    1.2.1、类适配器模式实现案例

    1.2.2、对象适配器

    1.2.3、接口适配器

    1.3、优缺点(对象适配器模式)

    1.4、应用场景


    一、适配器模式


    一句话概括结构式模式

    教你将类和对象结合再一起形成一个更强大的结构.

    1.1、适配器模式概述

    将一个类的接口转换成客户希望的另一个接口,使得原本不兼容的接口能一起工作.

    比如,如果你使用的是苹果手机,那么就意味着充电器的充电口也是苹果标准的,而你现在只有一个 type-c 插孔的插座能充电,因此就需要一个转换器(一头type-c,另一头 苹果插头),就可以让原本不兼容的 苹果插头 一起工作.

    适配器模式包含以下主机角色:

    1. 目标接口:当前客户所期待的接口,它可以是抽象类或者接口(上述的 苹果插头).
    2. 适配者类:是被访问的现存组件库中的接口(上述的 type-c).
    3. 适配器类:是一个转换器,通过继承或引用目标接口,实现适配者类的所有方法,就可以实现转换效果.

    适配器模式分为 类适配器模式、对象适配器模式 ,其中类适配器耦合度最高(不符合合成/聚合复用原则),且要求程序员了解现有组件库的内部结构,因此应用较少.

    还有一种模式叫 接口适配器模式,是对对象适配器的扩展.

    1.2、案例

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

    1.2.1、类适配器模式实现案例

    类适配器只需要我们继承目标接口,实现适配者接口的所有方法即可.

    1. /**
    2. * 目标接口: TF 卡
    3. */
    4. public interface TFCard {
    5. /**
    6. * 读取 TF 卡
    7. * @return
    8. */
    9. String readTF();
    10. /**
    11. * 写入 TF 卡
    12. */
    13. void writeTF(String msg);
    14. }
    1. /**
    2. * 目标接口实现类
    3. */
    4. public class TFCardImpl implements TFCard{
    5. @Override
    6. public String readTF() {
    7. String msg = "tf card readTF: hello!";
    8. return msg;
    9. }
    10. @Override
    11. public void writeTF(String msg) {
    12. System.out.println("tf card writeTF: hello!");
    13. }
    14. }
    1. /**
    2. * 适配者接口: SD 卡
    3. */
    4. public interface SDCard {
    5. /**
    6. * 读取 SD 卡
    7. * @return
    8. */
    9. String readSD();
    10. /**
    11. * 写入 SD 卡
    12. */
    13. void writeSD(String msg);
    14. }
    1. /**
    2. * 适配者实现类: SD 卡实现类
    3. */
    4. public class SDCardImpl implements SDCard {
    5. @Override
    6. public String readSD() {
    7. String msg = "sd card readTF: hello!";
    8. return msg;
    9. }
    10. @Override
    11. public void writeSD(String msg) {
    12. System.out.println("sd card writeTF: " + msg);
    13. }
    14. }
    1. /**
    2. * 适配器:SD 兼容 TF
    3. */
    4. public class SDAdapterTF extends TFCardImpl implements SDCard{
    5. @Override
    6. public String readSD() {
    7. System.out.println("adapter read tf card");
    8. return readTF();
    9. }
    10. @Override
    11. public void writeSD(String msg) {
    12. System.out.println("adapter write tf card");
    13. writeTF(msg);
    14. }
    15. }
    1. /**
    2. * 电脑类
    3. */
    4. public class Computer {
    5. public String readSD(SDCard sdCard) {
    6. if(sdCard == null) {
    7. throw new NullPointerException("sd card null");
    8. }
    9. return sdCard.readSD();
    10. }
    11. }
    1. public static void main(String[] args) {
    2. //1.创建一个电脑类
    3. Computer computer = new Computer();
    4. //3.通过适配器从电脑中读取 TF 卡的数据
    5. SDAdapterTF adapter = new SDAdapterTF();
    6. String msg = computer.readSD(adapter);
    7. System.out.println(msg);
    8. }

    1.2.2、对象适配器

    对象适配器,相比于 类适配器,更符合 合成/聚合复用原则(持有新对象的引用,而不是通过继承来达到复用目的).  也就是说,它是通过持有目标接口的引用(tf 卡接口的引用),重写 适配者接口 的所有方法实现的 .  

    1. /**
    2. * 目标接口: TF 卡
    3. */
    4. public interface TFCard {
    5. /**
    6. * 读取 TF 卡
    7. * @return
    8. */
    9. String readTF();
    10. /**
    11. * 写入 TF 卡
    12. */
    13. void writeTF(String msg);
    14. }
    1. /**
    2. * 目标接口实现类
    3. */
    4. public class TFCardImpl implements TFCard {
    5. @Override
    6. public String readTF() {
    7. String msg = "tf card readTF: hello!";
    8. return msg;
    9. }
    10. @Override
    11. public void writeTF(String msg) {
    12. System.out.println("tf card writeTF: hello!");
    13. }
    14. }
    1. /**
    2. * 适配者接口: SD 卡
    3. */
    4. public interface SDCard {
    5. /**
    6. * 读取 SD 卡
    7. * @return
    8. */
    9. String readSD();
    10. /**
    11. * 写入 SD 卡
    12. */
    13. void writeSD(String msg);
    14. }
    1. /**
    2. * 适配者实现类: SD 卡实现类
    3. */
    4. public class SDCardImpl implements SDCard {
    5. @Override
    6. public String readSD() {
    7. String msg = "sd card readTF: hello!";
    8. return msg;
    9. }
    10. @Override
    11. public void writeSD(String msg) {
    12. System.out.println("sd card writeTF: " + msg);
    13. }
    14. }
    1. /**
    2. * 适配器:SD 兼容 TF
    3. */
    4. public class SDAdapterTF implements SDCard {
    5. private TFCard tfCard;
    6. public SDAdapterTF(TFCard tfCard) {
    7. this.tfCard = tfCard;
    8. }
    9. @Override
    10. public String readSD() {
    11. System.out.println("adapter read tf card");
    12. return tfCard.readTF();
    13. }
    14. @Override
    15. public void writeSD(String msg) {
    16. System.out.println("adapter write tf card");
    17. tfCard.writeTF(msg);
    18. }
    19. }
    1. /**
    2. * 电脑类
    3. */
    4. public class Computer {
    5. public String readSD(SDCard sdCard) {
    6. if(sdCard == null) {
    7. throw new NullPointerException("sd card null");
    8. }
    9. return sdCard.readSD();
    10. }
    11. }
    1. public class Client {
    2. public static void main(String[] args) {
    3. //1.创建一个电脑类
    4. Computer computer = new Computer();
    5. //3.通过适配器从电脑中读取 TF 卡的数据
    6. SDAdapterTF adapter = new SDAdapterTF(new TFCardImpl());
    7. computer.readSD(adapter);
    8. }
    9. }

    1.2.3、接口适配器

    当我们不希望实现一个适配者接口(sd 卡接口)中的所有方法时,可以创建一个抽象类 Adapter,实现所有方法(不用实现方法内容).此时我们只需要继承该抽象类,在重写我们需要的方法即可.

    实现前两个适配器中,就一直没有使用 writeSD 方法,因此这里就不实现此方法.

    1. /**
    2. * 目标接口: TF 卡
    3. */
    4. public interface TFCard {
    5. /**
    6. * 读取 TF 卡
    7. * @return
    8. */
    9. String readTF();
    10. /**
    11. * 写入 TF 卡
    12. */
    13. void writeTF(String msg);
    14. }
    1. /**
    2. * 目标接口实现类
    3. */
    4. public class TFCardImpl implements TFCard {
    5. @Override
    6. public String readTF() {
    7. String msg = "tf card readTF: hello!";
    8. return msg;
    9. }
    10. @Override
    11. public void writeTF(String msg) {
    12. System.out.println("tf card writeTF: hello!");
    13. }
    14. }
    1. /**
    2. * 适配者接口: SD 卡
    3. */
    4. public interface SDCard {
    5. /**
    6. * 读取 SD 卡
    7. * @return
    8. */
    9. String readSD();
    10. /**
    11. * 写入 SD 卡
    12. */
    13. void writeSD(String msg);
    14. }
    1. /**
    2. * 适配者实现类: SD 卡实现类
    3. */
    4. public class SDCardImpl implements SDCard {
    5. @Override
    6. public String readSD() {
    7. String msg = "sd card readTF: hello!";
    8. return msg;
    9. }
    10. @Override
    11. public void writeSD(String msg) {
    12. System.out.println("sd card writeTF: " + msg);
    13. }
    14. }
    1. public abstract class Adapter implements SDCard {
    2. @Override
    3. public void writeSD(String msg) {
    4. }
    5. @Override
    6. public String readSD() {
    7. return null;
    8. }
    9. }
    1. public class SDAdapterTF extends Adapter implements SDCard{
    2. private TFCard tfCard;
    3. public SDAdapterTF(TFCard tfCard) {
    4. this.tfCard = tfCard;
    5. }
    6. @Override
    7. public String readSD() {
    8. System.out.println("adapter read tf card");
    9. return tfCard.readTF();
    10. }
    11. }
    1. /**
    2. * 电脑类
    3. */
    4. public class Computer {
    5. public String readSD(SDCard sdCard) {
    6. if(sdCard == null) {
    7. throw new NullPointerException("sd card null");
    8. }
    9. return sdCard.readSD();
    10. }
    11. }
    1. public class Client {
    2. public static void main(String[] args) {
    3. //1.创建一个电脑类
    4. Computer computer = new Computer();
    5. //2.通过适配器从电脑中读取 TF 卡的数据
    6. SDAdapterTF sdAdapterTF = new SDAdapterTF(new TFCardImpl());
    7. String msg = computer.readSD(sdAdapterTF);
    8. System.out.println(msg);
    9. }
    10. }

    1.3、优缺点(对象适配器模式)

    优点

    1. 适配现有类,且不修改类:在不改变现有类的基础上,实现现有类和目标类的接口的匹配.

    2. 符合 合成/聚合 复用原则:持有引用,而不继承.

    3. 符合开闭原则:如果引入新的目标接口,只需要在适配器类中进行扩展,不需要修改原代码.

    缺点:

    增加复杂性:编写适配器类时,要考虑全面,包括适配者和目标类.

    1.4、应用场景

    1. 以前开发的系统中存在满足当前业务所需要的类,但是接口和当前业务所需接口不一致.

    2. 第三方提供的组件,但是组件接口定义和自己要求的接口定义不同.

  • 相关阅读:
    真offer收割机 第二弹~大厂如何考察候选人?(附答案详解)
    【Matplotlib绘制图像大全】(二十四):Matplotlib为图像添加网格信息
    『无为则无心』Python面向对象 — 53、对Python中封装的介绍
    Flask 学习-61.Flask-Mail 发送邮件
    艾美捷AAT别藻蓝蛋白普通储备溶液制备方案
    ChatGPT架构师:语言大模型的多模态能力、幻觉与研究经验
    [需求管理-2]:什么是需求以及需求的收集与识别
    2022“杭电杯”中国大学生算法设计超级联赛(4)D、F、G、K
    华理生物冲刺科创板:年营收2.26亿 拟募资8亿
    python中的async和await
  • 原文地址:https://blog.csdn.net/CYK_byte/article/details/133696479