美文网首页
适配器模式

适配器模式

作者: 潜心之力 | 来源:发表于2020-08-04 16:35 被阅读0次

一、模式简介

定义:将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。
场景:使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同。

  • 角色结构:
  • 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
  • 适配者(Adaptee)类:它是被访问和适配的现存组件库中的组件接口。
  • 适配器(Adapter)类:它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。

二、模式实现

组合模式

public class ThreePlug { -> 适配接口,已存在,定义class
    public void power(){
        System.out.println("三相电源");
    }
}

public interface TwoPlug { -> 目标接口,不存在,定义interface
    void power();
}

public class FanAdapter implements TwoPlug { -> 排插适配器
    private ThreePlug threePlug;

    public FanAdapter(ThreePlug threePlug) {
        this.threePlug = threePlug;
    }

    @Override
    public void power() {
        threePlug.power();
        System.out.println("二相电源");
    }
}

public class Fan { -> 风扇实体
    private TwoPlug twoPlug;

    public Fan(TwoPlug twoPlug) {
        this.twoPlug = twoPlug;
    }

    public void power() {
        twoPlug.power();
    }
}
ThreePlug threePlug = new ThreePlug();
TwoPlug twoPlug = new FanAdapter(threePlug);
Fan fan = new Fan(twoPlug);
fan.power();

继承模式,在组装模式基础上改造适配器

public class FanAdapter extends ThreePlug implements TwoPlug {
    @Override
    public void power() {
        super.power();
        System.out.println("二相电源");
    }
}
TwoPlug twoPlug = new FanAdapter();
Fan fan = new Fan(twoPlug);
fan.power();

双向模式,既可以把适配者接口转换成目标接口,也可以把目标接口转换成适配者接口

public interface TwoPlug { -> 目标接口,通过适配器转化适配接口获得
    void twoPower();
}

public class TwoPlugExtends implements TwoPlug{
    @Override
    public void twoPower() {
        System.out.println("二相电源");
    }
}

public interface ThreePlug { -> 适配接口,通过适配器转为目标接口
    void threePower();
}

public class ThreePlugExtends implements ThreePlug{
    @Override
    public void threePower() {
        System.out.println("三相电源");
    }
}
public class Fan { -> 风扇类,使用二相接口
    private TwoPlug twoPlug;

    public Fan(TwoPlug twoPlug) {
        this.twoPlug = twoPlug;
    }

    public void twoPower() {
        twoPlug.twoPower();
    }
}
public class FanAdapter implements TwoPlug, ThreePlug { -> 风扇适配器,实现适配接口到目标接口的转换(排插)
    private Fan fan;

    private ThreePlug threePlug;

    public FanAdapter(Fan fan, ThreePlug threePlug) {
        this.fan = fan;
        this.threePlug = threePlug;
    }

    @Override
    public void threePower() {
        fan.twoPower();
    }

    @Override
    public void twoPower() {
        threePlug.threePower();
    }

    public Fan getFan() {
        return fan;
    }

    public void setFan(Fan fan) {
        this.fan = fan;
    }

    public ThreePlug getThreePlug() {
        return threePlug;
    }

    public void setThreePlug(ThreePlug threePlug) {
        this.threePlug = threePlug;
    }
}
TwoPlug twoPlug = new TwoPlugExtends();
ThreePlug threePlug = new ThreePlugExtends();
Fan fan = new Fan(twoPlug);
FanAdapter adapter = new FanAdapter(fan,threePlug);
adapter.twoPower(); -> 三相转二相
adapter.threePower(); -> 二相转三相

相关文章

  • 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...

  • 设计模式:结构型

    享元模式 (Pools,Message) 代理模式 适配器模式 :类适配器和对象适配器 装饰者模式 外观模式 桥接...

网友评论

      本文标题:适配器模式

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