设计模式-结构型-适配器模式(adapter)

1.概念

  • 将一个类的接口转换成客户希望的另外一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作;
  • 适配器模式分为类适配器模式对象适配器模式,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些;

2.结构

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

3.类适配器模式

实现方式:定义一个适配器类来实现当前系统的业务接口,同时又继承现有组件库中已经存在的组件;

示例:读卡器

(1)目标接口-SD卡

public interface SDCard {

    //从SD卡中读取数据
    String readSD();
    //往SD卡中写数据
    void writeSD(String msg);
}

(2)具体的实现类

public class SDCardImpl implements SDCard {

    public String readSD() {
        String msg = "SDCard read msg : hello word SD";
        return msg;
    }

    public void writeSD(String msg) {
        System.out.println("SDCard write msg :" + msg);
    }
}

(3)计算机类

public class Computer {

    //从SD卡中读取数据
    public String readSD(SDCard sdCard) {
        if(sdCard == null) {
            throw  new NullPointerException("sd card is not null");
        }
        return sdCard.readSD();
    }
}

(4)适配者类的接口

public interface TFCard {

    //从TF卡中读取数据
    String readTF();
    //往TF卡中写数据
    void writeTF(String msg);
}

实现类:

public class TFCardImpl implements TFCard {

    public String readTF() {
        String msg = "TFCard read msg : hello word TFcard";
        return msg;
    }

    public void writeTF(String msg) {
        System.out.println("TFCard write msg :" + msg);
    }
}

(5)适配器类

public class SDAdapterTF extends TFCardImpl implements SDCard {

    public String readSD() {
        System.out.println("adapter read tf card");
        return readTF();
    }

    public void writeSD(String msg) {
        System.out.println("adapter write tf card");
        writeTF(msg);
    }
}

(6)测试类

public class Client {
    public static void main(String[] args) {
        //创建计算机对象
        Computer computer = new Computer();
        //读取SD卡中的数据
        String msg = computer.readSD(new SDCardImpl());
        System.out.println(msg);

        System.out.println("===============");
        //使用该电脑读取TF卡中的数据
        //定义适配器类
        String msg1 = computer.readSD(new SDAdapterTF());
        System.out.println(msg1);
    }
}

运行结果:

  • 类适配器模式违背了合成复用原则(通过将已有的对象纳入新对象中,作为新对象的成员对象来实现的,新对象可以调用已有对象的功能,从而达到复用)。类适配器是客户类有一个接口规范的情况下可用,反之不可用;
  • 关于合成复用原则的小示例:

可以看出用继承关系实现会产生很多子类,而且增加新的“动力源”或者增加新的“颜色”都要修改源代码,这违背了开闭原则,显然不可取。但如果改用组合关系实现就能很好地解决以上问题:

4.对象适配器模式

实现方式:对象适配器模式可釆用将现有组件库中已经实现的组件引入适配器类中,该类同时实现当前系统的业务接口

示例:读卡器

类适配器模式的代码,我们只需要修改适配器类(SDAdapterTF)和测试类;

(1)适配器类

public class SDAdapterTF implements SDCard {

    //声明适配者类
    private TFCard tfCard;

    public SDAdapterTF(TFCard tfCard) {
        this.tfCard = tfCard;
    }

    public String readSD() {
        System.out.println("adapter read tf card");
        return tfCard.readTF();
    }

    public void writeSD(String msg) {
        System.out.println("adapter write tf card");
        tfCard.writeTF(msg);
    }
}

(2)测试类

public class Client {
    public static void main(String[] args) {
        //创建计算机对象
        Computer computer = new Computer();
        //读取SD卡中的数据
        String msg = computer.readSD(new SDCardImpl());
        System.out.println(msg);

        System.out.println("===============");
        //使用该电脑读取TF卡中的数据
        //创建适配器类对象
        SDAdapterTF sdAdapterTF = new SDAdapterTF(new TFCardImpl());
        String msg1 = computer.readSD(sdAdapterTF);
        System.out.println(msg1);
    }
}

注意:还有一个适配器模式是接口适配器模式。当不希望实现一个接口中所有的方法时,可以创建一个抽象类Adapter,实现所有方法。而此时我们只需要继承该抽象类即可。

5.应用场景

  • 以前开发的系统存在满足新系统功能需求的类,但其接口同新系统的接口不一致;
  • 使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同;

6.JDK源码解析

  • Reader(字符流)、InputStream(字节流)的适配使用的是InputStreamReader;
  • InputStreamReader继承自java.io包中的Reader,对他中的抽象的未实现的方法给出实现;
public int read() throws IOException {
    return sd.read();
    }
public int read(char cbuf[], int offset, int length) throws IOException {
    return sd.read(cbuf, offset, length);
}

如上代码中的sd(StreamDecoder类对象),在Sun的JDK实现中,实际的方法实现是对sun.nio.cs.StreamDecoder类的同名方法的调用封装。类结构图如下

  • InputStreamReader是对同样实现了Reader的StreamDecoder的封装;
  • StreamDecoder不是Java SE API中的内容,是Sun JDK给出的自身实现。但我们知道他们对构造方法中的字节流类(InputStream)进行封装,并通过该类进行了字节流和字符流之间的解码转换;
  • StreamDecoder采用了适配器模式;
发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章