适配器模式设计

适配器模式

定义:利用适配器解决接口不兼容问题,将一个接口适配成用户所期待的。

如下图所示,两接口的插头(目标对象)无法与三接头的插孔(适配者)适配,无法达到充电的效果。利用转换器可以实现两接口的插头与三接头的插孔适配,达到充电效果。其中转换器需要将三接头的插孔输出的电压按照一定的规则转换为两接口的插头需要的电压。我们以该事件为例,进行适配器模式的说明。

在这里插入图片描述

目标对象(两接头的插头)

public interface Target {
     int input(int msg);
}
public class TargetImpl implements Target{
    @Override
    public int input(int msg) {
        System.out.println("两接头插头输入电流《《《《《《《"+msg);
        return msg;
    }
}

适配者(三接头的插孔)

public interface Matches {
    int output(int msg);
}
public class MatchesImpl implements Matches {
    @Override
    public int output(int msg) {
        System.out.println("三接头插孔输出电流》》》》》》"+msg);
        return msg;
    }
}

//实现功能:发电站发电,通过三接头插孔向二接头插孔送电。

public class PowerStation {

    public void transfer(Matches matches){
         int power = matches.output(2);
         TargetImpl target = new TargetImpl();
         target.input(power);
    }

    public static void main(String[] args) {
        PowerStation powerStation = new PowerStation();
        MatchesImpl matches = new MatchesImpl();
        powerStation.transfer(matches);
    }
}

结果:

三接头插孔输出电流》》》》》》2
两接头插头输入电流《《《《《《《2

分析:

可以看出,由于接头的不匹配,导致三接头插孔两接头插头进行电路连通电电流依旧维持在2,造成电流过大,造成失败。

为了解决电压平衡问题(接口不兼容问题),我们引入适配器模式进行解决。

1.类适配器模式

**定义:由适配器类实现当前系统业务的接口,同时继承已存在的目标类。**大意就是通过适配器类实现抽象适配者的接口,利用抽象适配者提供的”壳子“通过一定的转换完成真正要进行的操作-目标类的方法。因此在适配器中应该存在三种操作:**壳子(实现的接口)、转换规则、真正的操作-目标类的方法(继承的目标类)。**具体实现如下。

适配器:

public class Matches2TargetAdaptor  extends TargetImpl implements Matches{

    @Override
    public int output(int msg) {
        System.out.println("三接头插孔输出电流》》》》》》"+msg);
        int s=msg-1;//接口不兼容的处理过程
        System.out.println("适配器转换后电流》》》》》"+s);
        int input = input(s);
        return input;
    }
}
public class PowerStation {

    public void transfer(Matches matches){
         matches.output(2);
    }

    public static void main(String[] args) {
        PowerStation powerStation = new PowerStation();
        Matches2TargetAdaptor matches2TargetAdaptor = new Matches2TargetAdaptor();
        powerStation.transfer(matches2TargetAdaptor);
    }
}

结果:

适配器转换前电流》》》》》2
适配器转换后电流》》》》》1
两接头插头输入电流《《《《《《《1

分析

可以从结果中看出,三接头插孔传输电压2,经过适配器的处理,使得输出电压达到1,能够承担两接头插头的传输。

2.对象适配器模式

定义:由适配器类实现当前系统业务的接口,将已存在的目标类以组合的方式存入到适配器中,由外部决定需要传入的目标对象。因此在适配器中应该存在三种操作:**壳子(实现的接口)、转换规则、真正的操作-目标类的方法(构造器注入的目标类)。**具体实现如下。

适配器:

public class Matches2TargetAdaptor implements Matches{

    private Target target;

    public Matches2TargetAdaptor(Target target) {
        this.target = target;
    }

    @Override
    public int output(int msg) {
        System.out.println("三接头插孔输出电流》》》》》》"+msg);
        int s=msg-1;
        System.out.println("适配器转换后电流》》》》》"+s);
        int input = target.input(s);
        return input;
    }
}
public class PowerStation {

    public void transfer(Matches matches){
         matches.output(2);
    }

    public static void main(String[] args) {
        PowerStation powerStation = new PowerStation();
        Matches2TargetAdaptor matches2TargetAdaptor = new Matches2TargetAdaptor(new TargetImpl());
        powerStation.transfer(matches2TargetAdaptor);
    }
}

结果:

适配器转换前电流》》》》》2
适配器转换后电流》》》》》1
两接头插头输入电流《《《《《《《1
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值