美文网首页
五、适配器模式

五、适配器模式

作者: 奔跑吧老王 | 来源:发表于2020-04-01 09:55 被阅读0次

适配器模式分类

类适配器 集成

对象适配器 组合替代集成 符合合成复用原则

接口适配器

本文展示的为对象适配器模式,话不多说上图:

image.png

220伏电压类

public class Voltage220 {


    public Integer outPut(){

        System.out.println(220+"伏");
        return 220;
    }
}

5伏接口类

public interface Voltage5 {
    int outputFive();
}

适配器类

@Data
public class VoltageAdapter implements Voltage5{


    private Voltage220 voltage220;
    @Override
    public int outputFive() {
        Integer integer = voltage220.outPut();
        return integer/44;
    }
}

手机调用类des

@Data
public class Phone implements  Voltage5{

    private Voltage5 voltage5;
    @Override
    public int outputFive() {
        int i = voltage5.outputFive();
        System.out.println("输出电压为:"+i);
        return i;
    }
}

上下文操作类:

public class Context {

    public static void main(String[] args) {
        Phone phone=new Phone();
        VoltageAdapter voltageAdapter = new VoltageAdapter();
        voltageAdapter.setVoltage220(new Voltage220());
        phone.setVoltage5(voltageAdapter);
        phone.outputFive();
    }
}

相关文章

  • 常用的Javascript设计模式

    一、构造函数模式 二、工厂模式 三、模块模式 四、混合模式 五、单例模式 六、发布订阅者模式 七、适配器模式适配器...

  • Java设计模式(二)

    talk is cheap show me the code 适配器模式 类适配器模式 接口适配器模式 对象适配器...

  • 适配器模式

    目录 1、什么是适配器模式? 2、适配器模式结构? 3、如何实现适配器模式? 4、适配器模式的特点? 5、适配器模...

  • 设计模式之适配器模式

    适配器模式: 类适配器模式、对象适配器模式、接口适配器模式 1.类适配器模式:新的接口出现了,但是和老的接口不兼容...

  • 学习iOS设计模式第一章 适配器(Adapter)

    今天学习了iOS设计模式中的适配器模式,适配器有两种模式对象适配器模式-- 在这种适配器模式中,适配器容纳一个它包...

  • 第4章 结构型模式-适配器模式

    一、适配器模式简介 二、适配器模式的优点 三、适配器模式的实例

  • 设计模式(Design Patterns)适配器模式(Adapt

    适配器模式主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。 类的适配器模式 场景:将一个类转换成...

  • 适配器模式

    适配器模式主要分为三类:类的适配器模式、对象的适配器模式、接口的适配器模式。适配器模式将某个类的接口转换成客户端期...

  • 适配器模式

    先直观感受下什么叫适配器 适配器模式有类的适配器模式和对象的适配器模式两种不同的形式。 类适配器模式 对象适配器模...

  • 适配器模式

    适配器模式 一、适配器模式定义 适配器模式的定义是,Convert the interface of a clas...

网友评论

      本文标题:五、适配器模式

      本文链接:https://www.haomeiwen.com/subject/fqptphtx.html