• JAVA设计模式之适配器模式


    JAVA设计模式之适配器模式

    1. 定义

    适配器模式主要用于将一个类的接口转化成客户端希望的目标类格式,使得原本不兼容的类可以在一起工作,将目标类和适配者类解耦;同时也符合“开闭原则”,可以在不修改原代码的基础上增加新的适配器类;将具体的实现封装在适配者类中,对于客户端类来说是透明的,而且提高了适配者的复用性,但是缺点在于更换适配器的实现过程比较复杂。

    2. 类型

    结构型

    3. 场景

    已存在的类,它的方法和需求不匹配时(方法结果相同或相似)

    4. 优点

    提高类的透明性和复用,现有的类复用但不需要改变

    目标类和适配器类解耦,提高程序扩展性

    符合开闭原则

    5. 缺点

    适配器编写过程需要全面考虑,会增加系统的复杂性

    增加系统阅读的阅读行

    6. 适配器扩展

    对象适配器

    类适配器

    6. 相关适配器模式

    1. 适配器模式和外观模式

    适配器模式和外观模式都是对现有系统的封装,外观模式是定义新的接口,适配器模式是复用原有的接口,适配器模式是使已有的两个接口协同工作,而外观模式是提供一个更为方便的入口。

    7. coding

    7.1 被 适配者方法

    package com.maidou.learning.design.structure.adapter.classadapter;
    
    public class Adaptee {
    
        public void adapteeRequest() {
            System.out.println("被适配者");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7.2 接口

    package com.maidou.learning.design.structure.adapter.classadapter;
    
    public interface Target {
    
        void request();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    7.3 接口的实现

    package com.maidou.learning.design.structure.adapter.classadapter;
    
    public class ConcreteTarget implements Target{
        @Override
        public void request() {
            System.out.println("被适配者");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    7.4 适配者适配被适配者达到Target 目标

    package com.maidou.learning.design.structure.adapter.classadapter;
    
    public class Adapter extends Adaptee implements Target{
        @Override
        public void request() {
            super.adapteeRequest();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7xfZeT6X-1666707488893)(C:\Users\maido\AppData\Roaming\Typora\typora-user-images\image-20221025213821425.png)]

    测试

    package com.maidou.learning.design.structure.adapter.classadapter;
    
    public class MainTest {
    
        public static void main(String[] args) {
            Target target = new ConcreteTarget();
            target.request();
    
            Target target1 = new Adapter();
            target1.request();
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    7.5 类适配器的实现方式,其他的都不变

    package com.maidou.learning.design.structure.adapt1er.objectadapter;
    
    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
    • 10
    • 11
    • 12

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-XacAZwCS-1666707488898)(C:\Users\maido\AppData\Roaming\Typora\typora-user-images\image-20221025220134250.png)]

    8. 源码解析

    8.1 XmlAdapter

    8.1.1来源 javax.xml.bind.annotation.adapters
    8.1.2coding
       protected XmlAdapter() {}
    
        /**
         * Convert a value type to a bound type.
         *
         * @param v
         *      The value to be converted. Can be null.
         * @throws Exception
         *      if there's an error during the conversion. The caller is responsible for
         *      reporting the error to the user through {@link    javax.xml.bind.ValidationEventHandler}.
         */
        public abstract BoundType unmarshal(ValueType v) throws Exception;
    
        /**
         * Convert a bound type to a value type.
         *
         * @param v
         *      The value to be convereted. Can be null.
         * @throws Exception
         *      if there's an error during the conversion. The caller is responsible for
         *      reporting the error to the user through {@link javax.xml.bind.ValidationEventHandler}.
         */
        public abstract ValueType marshal(BoundType v) throws Exception;   
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    8.1.3 说明

    实行xml的序列化方法和反序列方法进行数据的传输

    8.2 AdvisorAdapter

    8.2.1 来源
    org.springframework.aop.framework.adapter
    
    • 1
    8.2.2 coding
    public interface AdvisorAdapter {
        boolean supportsAdvice(Advice advice);
    
        MethodInterceptor getInterceptor(Advisor advisor);
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

    实现方法

    class MethodBeforeAdviceAdapter implements AdvisorAdapter, Serializable {
        MethodBeforeAdviceAdapter() {
        }
    
        public boolean supportsAdvice(Advice advice) {
            return advice instanceof MethodBeforeAdvice;
        }
    
        public MethodInterceptor getInterceptor(Advisor advisor) {
            MethodBeforeAdvice advice = (MethodBeforeAdvice)advisor.getAdvice();
            return new MethodBeforeAdviceInterceptor(advice);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    8.3 JpaVendorAdapter

    8.3.1 来源
    org.springframework.orm.jpa
    
    • 1
    8.3.2 coding
    public interface JpaVendorAdapter {
        PersistenceProvider getPersistenceProvider();
    
        @Nullable
        default String getPersistenceProviderRootPackage() {
            return null;
        }
    
        default Map<String, ?> getJpaPropertyMap(PersistenceUnitInfo pui) {
            return this.getJpaPropertyMap();
        }
    
        default Map<String, ?> getJpaPropertyMap() {
            return Collections.emptyMap();
        }
    
        @Nullable
        default JpaDialect getJpaDialect() {
            return null;
        }
    
        default Class<? extends EntityManagerFactory> getEntityManagerFactoryInterface() {
            return EntityManagerFactory.class;
        }
    
        default Class<? extends EntityManager> getEntityManagerInterface() {
            return EntityManager.class;
        }
    
        default void postProcessEntityManagerFactory(EntityManagerFactory emf) {
        }
    
        default void postProcessEntityManager(EntityManager em) {
        }
    } 
    
    • 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
    • 31
    • 32
    • 33
    • 34
    • 35
  • 相关阅读:
    N点复序列求2个N点实序列的快速傅里叶变换
    【数据结构】二叉树经典例题---<你真的掌握二叉树了吗?>(第二弹)
    批量发送邮件时怎么使用蜂邮EDM与Outlook?
    java泛型场景补充注意事项
    【Nginx34】Nginx学习:安全链接、范围分片以及请求分流模块
    前端一阶段进入二阶段后的学习如何进行?
    【JVM技术专题】Thread的stackSize与-Xss参数的区别「分析篇」
    纵横职场的8招秘诀,高手都这么干
    PHP校园失物招领网站系统mysql
    大气环境一站式技能提升:SMOKE-CMAQ实践技术
  • 原文地址:https://blog.csdn.net/hyzsuccess/article/details/127494542