适配器模式

简介

* 定义:
    * 将一个类的接口, 转换成客户希望的另一个类的接口, 使原本不兼容的接口可以一起工作;
    * 属于结构性设计模式;

* 举例:
    * 电源适配器;

* 适用场景:
    * 不同产品/不同厂家提供的接口不同, 功能类似;
    * 已经存在的类, 他的方法和需求不匹配, 但是功能类似的情况;
    * 也就是说: 把一个某一功能, 转化为功能相似的另一功能;
    * 适配器模式一般和策略模式/委派模式/工厂模式结合使用;

* 适配器模式在spring中的应用:
    * 抽象适配器AdvisorAdapter: 面向切面变成的通知
        * 抽象适配器, 有通知功能;
    * 适配器:  MethodBeforeAdviceAdapter、AfterReturningAdviceAdapter 、ThrowsAdviceAdapter;
        * 适配为前置通知, 后置通知, 环绕通知;

* 适配器模式优点:
    * 提高代码复用性;
    * 目标类和适配类解耦;
    * 符合开闭原则;
* 适配器模式缺点:
    * 如果大量使用适配器模式, 代码阅读性会大幅度降低;

* 三种适配器模式:
    * 对象适配器;
    * 类适配器;
    * 抽象适配器;

对象适配器

简介
* 通过持有对象方式, 为方法提供适配;
代码
介绍
* 220V输入输出, 转换为220V输入,5V输出;
CODE
  • 220V target
public class AC220V {
    
    public int output(){
        return 220;
    }
}
  • 5V Adapter

    AC220V ac220 ;
    public DC5VAdapterForObject(AC220V ac220) {
        // TODO Auto-generated constructor stub
        this.ac220 = ac220;
    }
    
    public int input(){
        return ac220.output();
    }
    
    public int output5V(){
        return ac220.output()/4;
    }
}
  • test
public static void main(String[] args) {
        
        AC220V ac220 = new AC220V();
        DC5VAdapterForObject adapter = new DC5VAdapterForObject(ac220);
        System.out.println("input:" + adapter.input());
        System.out.println("output:" + adapter.output5V());
    }

类适配器

简介
* 通过继承的方式, 为方法提供适配;
CODE
介绍
* 220V输入输出, 转换为220V输入,5V输出;
* 220V和对象适配器通用;
  • 5V Adapter
public class DC5VAdapterForClass extends AC220V{

    public int output5V(){
        return output()/4;
    }
    
    public int input(){
        return output();
    }
}
  • test
    public static void main(String[] args) {
        DC5VAdapterForClass adapter = new DC5VAdapterForClass();
        System.out.println("input:" + adapter.input());
        System.out.println("output:" + adapter.output5V());
    }

抽象适配器

介绍
* 提供一个抽象适配器, 具备适配器的基础功能;
* 具体适配器实现抽象适配器, 适配指定方法:
    * 只需要关心 需要适配的抽象方法即可;

* 抽象适配器注意钩子的使用:
    * 因为一个抽象适配器会存在多个实现;
CODE
* 220V输入输出, 转换为220V输入,5V输出;
* 220V和对象适配器通用;
  • abstract adapter
public abstract class PowerAdapter {

    AC220V ac220;
    public PowerAdapter(AC220V ac220) {
        // TODO Auto-generated constructor stub
        this.ac220 = ac220;
    }
    
    //只需面向抽象实现;
    public abstract int output5V();
    
    public int output220V(){
        return 220;
    }
}
  • 5V adapter
public class DC5VAdapterForInterface extends PowerAdapter{

    public DC5VAdapterForInterface(AC220V ac220) {
        super(ac220);
    }

    //钩子
    public static boolean support(Object powerAdapter){
        return powerAdapter instanceof DC5VAdapterForInterface;
    }
    
    @Override
    public int output5V() {
        return super.output220V()/4;
    }

}
  • test
    public static void main(String[] args) {
        PowerAdapter adapter = new DC5VAdapterForInterface(new AC220V());
        if(DC5VAdapterForInterface.support(adapter)){
            System.out.println(adapter.output5V());
        }
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值