• 当设计模式遇上万象:探秘适配器模式的神奇变身



    一、概念

    适配器模式(Adapter Pattern)又叫做变压器模式,它的功能是将一个类的接口变成客户端所期待的另一种接口,从而使原本接口不匹配而无法在一起工作的两个类能在一起工作。属于结构型模式

    二、角色

    适配器模一般包含三种角色:
    目标角色(Target):也就是我们期望的接口;
    源角色(Adaptee):存在于系统中,内容满足客户需求(需转换),但接口不匹配的接口实例:
    适配器(Adapter):将源角色(Adaptee)转化为目标角色(Target)的类实例;

    三种形式

    场景:在中国民用电都是220V交流电,但我们收集使用的锂电池使用的5V直流电。因此我们给手机充电时就需要使用电源适配器来进行转换。

    1. 类适配器

    通过继承来实现适配器功能。

    Adaptee角色,需要被转换的对象AC220类,表示220V交流电:

    public class AC220 {
        public int outputAC220V(){
            int output = 220;
            System.out.println("输出电压"+output+"V");
            return output;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    Target角色DC5接口,表示5V直流电的标准:

    public interface DC5 {
        int output5V();
    }
    
    • 1
    • 2
    • 3

    创建Adapter角色电源适配器PowerAdapter类:

    public class PowerAdapter extends AC220 implements DC5{
        @Override
        public int output5V() {
            int adapterInput=super.outputAC220V();
            int adapterOutput=adapterInput/44;
            System.out.println("使用Adapter输入AC"+adapterInput+"V,输出DC"+adapterOutput+"V");
            return adapterOutput;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    客户端测试代码:

    public class Test {
        public static void main(String[] args) {
           DC5 powerAdapter = new PowerAdapter();
           powerAdapter.output5V();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    对象适配器

    通过持有Adaptee对象实现适配器功能。具体做法:Adapter类实现Target接口,然后内部持有Adaptee实例,然后再Target接口规定的方法内转换Adaptee。

    public class PowerAdapter  implements DC5 {
        private AC220 ac220;
        @SuppressWarnings("all")
        public PowerAdapter(AC220 ac220){
            this.ac220 = ac220;
        }
        @Override
        public int output5V() {
           int adapterInput = ac220.outputAC220V();
           int adapterOutput = adapterInput / 44;
            System.out.println("使用Adapter 输入AC"+adapterInput+"V,输出DC"+adapterOutput+"V");
            return adapterOutput;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    接口适配器

    创建Target角色DC类:

    public interface DC {
        int output5V();
        int ouptut12V();
        int output24V();
        int output36V();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    创建Adaptee角色AC220类:

    public class AC220 {
        public int outputAC220V() {
            int output=220;
            System.out.println("输出电压"+220+"V");
            return output;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    创建Adapter角色PowerAdapter类:

    public class PowerAdapter implements DC{
        private AC220 ac220;
        @SuppressWarnings("all")
        public PowerAdapter(AC220 ac220){
            this.ac220 = ac220;
        }
    
        @Override
        public int output5V() {
            int adapterInput = ac220.outputAC220V();
            int adapterOutput = adapterInput/44;
            System.out.println("使用Adapter输入AC"+adapterInput+"V,输出DC"+adapterOutput+"V");
            return adapterOutput ;
        }
    
        @Override
        public int ouptut12V() {
            return 0;
        }
    
        @Override
        public int output24V() {
            return 0;
        }
    
        @Override
        public int output36V() {
            return 0;
        }
    }
    
    • 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
    • 27
    • 28
    • 29
    • 30

    客户端代码:

    public class Test {
        public static void main(String[] args) {
            DC adapter = new PowerAdapter(new AC220());
            adapter.output5V();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    优缺点

    优点:

    1. 能提高类的透明性和复用,现有的类复用但不需要改变。
    2. 目标类和适配器类解耦,提高程序的扩展性。
    3. 在很多业务场景中符合开闭原则。

    缺点

    1. 适配器编写过程需要全面考虑,可能会增加系统的复杂性。
    2. 增加代码阅读难度,降低代码可读性,过多使用适配器会使系统代码变得凌乱。
  • 相关阅读:
    Python采集招聘数据信息(+详情页)并实现可视化
    20230916后台面经整理
    Jni GetMethodID中函数标识sig的详细解释
    ESP8266-Arduino编程实例-MCP23017并行IO扩展驱动
    mysql5.6 修改密码
    回归预测 | MATLAB实现RUN-XGBoost龙格库塔优化极限梯度提升树多输入回归预测
    Spring Boot对接RocketMQ示例
    CocosCreator3.8研究笔记(十三)CocosCreator 音频资源理解
    LLMs在股票投资组合崩溃中的时间关系推理
    排查服务器cpu运行过高
  • 原文地址:https://blog.csdn.net/Hi_alan/article/details/134424452