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


    定义:
    将一个类的接口转换成客户期望的另一个接口
    使原本接口不兼容的类可以一起工作
    适用场景:
    已经存在的类,他的方法和需求不匹配时(方法结果相同或相似)
    不是软件设计阶段考虑的设计模式,是随着软件维护,由于不同产品、不同厂家造成功能类似而接口不相同情况下的解决方案
    优点:
    能提高类的透明性和复用,现有的类复用但不需要改变
    目标类和适配器类解耦,提高程序扩展性
    符合开闭原则
    缺点:
    适配器编写过程需要全面考虑,可能会增加系统的复杂性
    增加系统代码可读的难度
    扩展:
    对象适配器
    类适配器
    相关设计模式:
    适配器模式和外观模式

    Coding

    public interface Target {
    
        void request();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public class ConcreteTarget implements Target {
        @Override
        public void request() {
            System.out.println("concreteTarget 目标方法");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    public class Test {
    
        public static void main(String[] args) {
        	//正常的调用
            Target target = new ConcreteTarget();
            target.request();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    加入适配器:
    类适配器模式(继承关系):

    public class Adaptee {
    
        public void adapteeRequest() {
            System.out.println("被适配者的方法");
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    public class Adapter extends Adaptee implements Target {
        @Override
        public void request() {
        	//在父类方法上 进行新的业务逻辑的调用
            super.adapteeRequest();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    public class Test {
    
        public static void main(String[] args) {
            Target adapterTarget = new Adapter();
            adapterTarget.request();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    对象适配器模式(组合关系):

    public class Adapter implements Target {
    
        private Adaptee adaptee = new Adaptee();
    
        @Override
        public void request() {
            adaptee.adapteeRequest();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    public class Test {
    
        public static void main(String[] args) {
            Target adapterTarget = new Adapter();
            adapterTarget.request();
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    一个案例,将220V电压转为5V电压

    public interface IAC {
    
        int outputACV();
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    public class AC220 implements IAC {
    
        @Override
        //正常220V的输出
        public int outputACV() {
            int output = 220;
            return output;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    public class PowerAdapter implements IAC {
    
        private IAC ac = new AC220();
    
        @Override
        //变压器功能,将220V的输出 转为 5V的输出
        public int outputACV() {
            int acv = ac.outputACV();
            //变压器
            System.out.println("输入电压为" + acv + "V");
            acv = acv / 44;
            System.out.println("输出电压为" + acv + "V");
            return acv;
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    public class Test {
    
        public static void main(String[] args) {
            IAC dc5 = new PowerAdapter();
            System.out.println(dc5.outputACV());
        }
    
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    Spring中org.springframework.aop.framework.adapter.MethodBeforeAdviceAdapterc 以及 org.springframework.orm.jpa.vendor.AbstractJpaVendorAdapter 采用的就是适配器模式

    todo mvc调用Springboot处理流程:
    1、http
    2、org.springframework.web.servlet.DispatcherServlet#doService
    3、org.springframework.web.servlet.DispatcherServlet#doDispatch
    4、org.springframework.web.servlet.DispatcherServlet#getHandlerAdapter //拿到支持的HandlerAdapter
    5、org.springframework.web.servlet.HandlerAdapter#handle //执行对应的调用

  • 相关阅读:
    Python 正则表达式进阶与实战
    Java 正则表达式字符篇
    MongoDB综合实战篇(超容易)
    微信小程序 nodejs+vue+uninapp学生在线选课作业管理系统
    Codeforces Round #801 (Div. 2) and EPIC Institute of Technology Round(C,D题解)
    详解KubeEdge EdgeMesh v1.15 边缘CNI特性
    java和数据库之间的关系,看这一篇就足够~~~
    web-3(httpd2.4)
    大学宿舍IP一键视频对讲
    springboot集成rabbitmmq多数据源,解决对源码不熟悉导致多个源出现同样队列,交换机等问题
  • 原文地址:https://blog.csdn.net/qq_36364521/article/details/125878750