• 设计模式--适配器模式


    适配器设计模式-----类对象结构型模式

    适配器模式(Adapter Pattern)是作为两个不兼容的接口之间的桥梁。这种类型的设计模式属于结构型模式,它结合了两个独立接口的功能。

    这种模式涉及到一个单一的类,该类负责加入独立的或不兼容的接口功能。举个真实的例子,读卡器是作为内存卡和笔记本之间的适配器。您将内存卡插入读卡器,再将读卡器插入笔记本,这样就可以通过笔记本来读取内存卡。

    意图:

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

    主要解决:

    主要解决在软件系统中,常常要将一些"现存的对象"放到新的环境中,而新环境要求的接口是现对象不能满足的。

    何时使用:

    1、系统需要使用现有的类,而此类的接口不符合系统的需要。 2、想要建立一个可以重复使用的类,用于与一些彼此之间没有太大关联的一些类,包括一些可能在将来引进的类一起工作,这些源类不一定有一致的接口。 3、通过接口转换,将一个类插入另一个类系中。(比如老虎和飞禽,现在多了一个飞虎,在不增加实体的需求下,增加一个适配器,在里面包容一个虎对象,实现飞的接口。)

    结构

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

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

    继承或依赖(推荐)。

    关键代码:

    适配器继承或依赖已有的对象,实现想要的目标接口。

    优点:

    1、可以让任何两个没有关联的类一起运行。 2、提高了类的复用。 3、增加了类的透明度。 4、灵活性好。

    缺点:

    1、过多地使用适配器,会让系统非常零乱,不易整体进行把握。比如,明明看到调用的是 A 接口,其实内部被适配成了 B 接口的实现,一个系统如果太多出现这种情况,无异于一场灾难。因此如果不是很有必要,可以不使用适配器,而是直接对系统进行重构。 2.由于 JAVA 至多继承一个类,所以至多只能适配一个适配者类,而且目标类必须是抽象类。

    使用场景:

    有动机地修改一个正常运行的系统的接口,这时应该考虑使用适配器模式。

    注意事项:

    适配器不是在详细设计时添加的,而是解决正在服役的项目的问题。

    代码:读卡器案例

    在这里插入图片描述

    1.创建 SDCard 接口

    /**
     * 目标接口
     */
    
    public interface SDCard {
    
        //从 SD 卡中读取数据
        String readSD();
    
        //往 SD 卡中写数据
        void writeSD(String msg);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2.创建 SDCardImal 类,实现 SDCard 接口

    /**
     * 具体的 SD 卡
     */
    public class SDCardImp implements SDCard {
        @Override
        public String readSD() {
            String msg = "SDCard read msg: hello world";
            return msg;
        }
    
        @Override
        public void writeSD(String msg) {
            System.out.println("SDCard write msg:" + msg);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    3.创建 Computer 类,用于实现读卡器在电脑上使用的逻辑

    /**
     * 计算机类
     */
    public class Computer {
    
        //从 SD 卡中读取数据
        public String readSD(SDCard sdCard) {
            if (sdCard == null) {
                throw new NullPointerException("sd card is not null!");
            }
            return sdCard.readSD();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    4.创建测试类 Client,进行测试

    public class Client {
        public static void main(String[] args) {
            //创建计算机对象
            Computer computer = new Computer();
    
            //读取 SD 卡中的数据
            String msg = computer.readSD(new SDCardImp());
            System.out.println(msg);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    5.创建 TFCard 接口

    
    /**
     * 适配者类的接口
     */
    public interface TFCard {
    
        //从TF卡中读取数据
        String readTF();
    
        //从TF卡中写数据
        void writeTF(String msg);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    1. 创建 TFCardImal 类,实现 TFCard 接口
    /**
     * 适配者类
     */
    public class TFCardImpl implements TFCard{
        @Override
        public String readTF() {
            String msg = "TFCard read msg:hello world1";
            return msg;
        }
    
        @Override
        public void writeTF(String msg) {
            System.out.println("tfCard write msg:" + msg);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    7.创建 TFAdapterSD 类,用作适配器

    
    /**
     * 适配器类
     */
    public class SDAdapterTF extends TFCardImpl implements SDCard {
        @Override
        public String readSD() {
            System.out.println("adater read tf card!");
            return readTF();
        }
    
        @Override
        public void writeSD(String msg) {
            System.out.println("adapter write tf card!");
            writeTF(msg);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    8.总的测试类

    public class Client {
        public static void main(String[] args) {
            //创建计算机对象
            Computer computer = new Computer();
    
            //读取 SD 卡中的数据
            String msg = computer.readSD(new SDCardImp());
            System.out.println(msg);
    
            System.out.println("=====================================");
    
            //使用该电脑读取 TF 卡中的数据
            //定义适配器类
            String msgl = computer.readSD(new SDAdapterTF());
            System.out.println(msgl);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    优化为对象适配器:上面代码违反了合成复用原则

    在这里插入图片描述

    将 TFCard 接口聚合到 SDAdapter 中
    将 SDAdapter 修改为如下代码:

    //只实现 SDCard 接口
    public class SDAdapterTF implements SDCard {
    
        //声明适配着类
        private TFCard tfCard;
    
        //有参构造
        public SDAdapterTF(TFCard tfCard) {
            this.tfCard = tfCard;
        }
    
    
        @Override
        public String readSD() {
            System.out.println("适配读取 TF 卡");
            return tfCard.readTF();
        }
    
        @Override
        public void writeSD() {
            System.out.println("适配读入 TF 卡");
            tfCard.writeTF();
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    测试类

    /**
     * 客户端
     */
    public class Client {
        public static void main(String[] args) {
            //创建计算机对象
            Computer computer = new Computer();
            //读取 SD 卡中的数据
            SDcardImpl sDcard = new SDcardImpl();
            // SDcardImpl 是 SDcard 的实现类,可以传入
            String readSD = computer.readSD(sDcard);
    
            System.out.println(readSD);
    
            System.out.println("==================================");
    
            //使用该电脑读取 TF 卡中的数据
            //创建适配器类对象
    
            SDAdapterTF sdAdapterTF = new SDAdapterTF(new TFCardImpl());
            String msg1 = computer.readSD(sdAdapterTF);
            System.out.println(msg1);
    
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    本文很多东西是取自菜鸟教程:https://www.runoob.com/design-pattern/adapter-pattern.html
  • 相关阅读:
    转置卷积理论解释(输入输出大小分析)
    Apache Jmeter压力测试与性能监控,监测cpu、内存、磁盘、网络
    【编程题】【Scratch一级】2022.06 报时的公鸡
    技术总结: PPT绘图
    11 盛水最多的容器
    用winsw将nodejs项目的exe程序安装为服务
    军事ar虚拟现实电子沙盘系统的功能
    TVTK-SV02 数据管线简介
    灵感乍现!造了个与众不同的Dubbo注册中心扩展轮子
    Clusterpedia 使用心得
  • 原文地址:https://blog.csdn.net/qq_44125965/article/details/126729940