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("被适配者");
    }
}

7.2 接口

package com.maidou.learning.design.structure.adapter.classadapter;

public interface Target {

    void request();
}

7.3 接口的实现

package com.maidou.learning.design.structure.adapter.classadapter;

public class ConcreteTarget implements Target{
    @Override
    public void request() {
        System.out.println("被适配者");
    }
}

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

package com.maidou.learning.design.structure.adapter.classadapter;

public class Adapter extends Adaptee implements Target{
    @Override
    public void request() {
        super.adapteeRequest();
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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();
    }
}

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();
    }
}

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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;   
8.1.3 说明

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

8.2 AdvisorAdapter

8.2.1 来源
org.springframework.aop.framework.adapter
8.2.2 coding
public interface AdvisorAdapter {
    boolean supportsAdvice(Advice advice);

    MethodInterceptor getInterceptor(Advisor advisor);
}

在这里插入图片描述

实现方法

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);
    }
}

8.3 JpaVendorAdapter

8.3.1 来源
org.springframework.orm.jpa
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) {
    }
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值