适配器模式

将一个类的接口转换成客户希望的另一个接口,使得原本由于接口不兼容而不能一起工作的那些类能一起工作。

适配器模式分为类适配器模式和对象适配器模式,前者类之间的耦合度比后者高,且要求程序员了解现有组件库中的相关组件的内部结构,所以应用相对较少些。

适配器模式的主要角色

目标接口(Target):当前系统业务所期待的接口,它可以是抽象类或接口

适配者类(Adaptee):它是被访问和适配的现存组件库中的组件接口。

适配器类(Adapter):它是一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。

类适配器模式

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

例子:读卡器,现有一台电脑只能读取SD卡,而要读取TF卡中的内容就需要使用到适配器模式。创建一个读卡器,将TF卡中的内容读取出来。

类图

java 设计模式适配器 和策略模式的区别 适配器设计模式类图_System

 

public interface SDCard {
     String readSD();
     void writeSD(String msg);
}


public class SDCardImpl implements SDCard{

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

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

public interface TFCard {
    String readTF();
    void writeTF(String msg);
}
public class TFCardImpl implements TFCard{

    @Override
    public String readTF() {
        String msg = "TFCard read msg:hello world TFCard";
        return msg;
    }

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

public class SDAdapter extends TFCardImpl implements SDCard {
    @Override
    public String readSD() {
        System.out.println("adapter read TF card");
        return readTF();
    }

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

}

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

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 SDAdapter());
        System.out.println(msg1);
    }
}

结果
SDCard read msg: hello world SD
------------------
adapter read TF card
TFCard read msg:hello world TFCard

Process finished with exit code 0

类适配器模式违背了合成复用原则,类适配器是客户类有一个接口规范的情况下可用,反之不可用。

对象适配器模式

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

java 设计模式适配器 和策略模式的区别 适配器设计模式类图_System_02

 

//适配者类的接口
public interface TFCard {
    String readTF();
    void writeTF(String msg);
}

public class TFCardImpl implements TFCard {

    @Override
    public String readTF() {
        String msg = "TFCard read msg:hello world TFCard";
        return msg;
    }

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

public interface SDCard {
     String readSD();
     void writeSD(String msg);
}

public class SDCardImpl implements SDCard {

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

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

public class SDAdapter implements SDCard {
    //声明适配者类
    private TFCard tfCard;

    public SDAdapter(TFCard tfCard){
        this.tfCard=tfCard;
    }
    @Override
    public String readSD() {
        System.out.println("adapter read tf card");
        return tfCard.readTF();
    }

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

}

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

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卡中的数据
        //创建适配器类对象
        SDAdapter sdAdapter = new SDAdapter(new TFCardImpl());
        String msg1 = computer.readSD(sdAdapter);
        System.out.println(msg1);
    }
}
结果
SDCard read msg: hello world SD
------------------
adapter read tf card
TFCard read msg:hello world TFCard

Process finished with exit code 0

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

适配器模式的应用场景

以前开发的系统存在满足系统功能需求的类,但接口同新系统的接口不一致

使用第三方提供的组件,但组件接口定义和自己要求的接口定义不同

JDK中使用适配器模式的例子

Reader(字符流)、InputStream(字节流)的适配使用的是InputStreamReader

InputStreamReader继承自java.io包中的Reader,对它抽象的未实现的方法给出了实现。

java 设计模式适配器 和策略模式的区别 适配器设计模式类图_System_03

 从图中可以看出

InputStreamReader是对同样实现了Reader的StreamDecoder的封装。

StreamDecoder不是Java SE API中的内容,是Sun JDK给出的自身实现。但我们知道他们对构造方法中的字节流类(InputStream)进行封装,并通过该类进行了字节流和字符流之间的解码转换。

结论:从表层上看,InputStreamReader做了InputStream字节流类到Reader字符流之间的转换,而从如上Sun JDK中的实现类关系结构中可以看到,是StreamDecoder的设计实现再实际上采用了适配器模式。