设计模式 - 结构型模式考点篇:适配器模式(类适配器、对象适配器、接口适配器)
  TEZNKK3IfmPf 2024年03月30日 53 0

一、适配器模式


一句话概括结构式模式

教你将类和对象结合再一起形成一个更强大的结构.

1.1、适配器模式概述

将一个类的接口转换成客户希望的另一个接口,使得原本不兼容的接口能一起工作.

比如,如果你使用的是苹果手机,那么就意味着充电器的充电口也是苹果标准的,而你现在只有一个 type-c 插孔的插座能充电,因此就需要一个转换器(一头type-c,另一头 苹果插头),就可以让原本不兼容的 苹果插头 一起工作.

适配器模式包含以下主机角色:

  1. 目标接口:当前客户所期待的接口,它可以是抽象类或者接口(上述的 苹果插头).
  2. 适配者类:是被访问的现存组件库中的接口(上述的 type-c).
  3. 适配器类:是一个转换器,通过继承或引用目标接口,实现适配者类的所有方法,就可以实现转换效果.

适配器模式分为 类适配器模式、对象适配器模式 ,其中类适配器耦合度最高(不符合合成/聚合复用原则),且要求程序员了解现有组件库的内部结构,因此应用较少.

还有一种模式叫 接口适配器模式,是对对象适配器的扩展.

1.2、案例

现有一台电脑,只能读取 SD 卡,而我现在只有一个 TF 卡,因此就需要使用适配器模式. 创建一个读卡器,将 TF 卡中的内容读取出来.

1.2.1、类适配器模式实现案例

类适配器只需要我们继承目标接口,实现适配者接口的所有方法即可.

/**
 * 目标接口: TF 卡
 */
public interface TFCard {

    /**
     * 读取 TF 卡
     * @return
     */
    String readTF();

    /**
     * 写入 TF 卡
     */
    void writeTF(String msg);

}
/**
 * 目标接口实现类
 */
public class TFCardImpl implements TFCard{

    @Override
    public String readTF() {
        String msg = "tf card readTF: hello!";
        return msg;
    }

    @Override
    public void writeTF(String msg) {
        System.out.println("tf card writeTF: hello!");
    }

}
/**
 * 适配者接口: SD 卡
 */
public interface SDCard {

    /**
     * 读取 SD 卡
     * @return
     */
    String readSD();

    /**
     * 写入 SD 卡
     */
    void writeSD(String msg);

}
/**
 * 适配者实现类: SD 卡实现类
 */
public class SDCardImpl implements SDCard {

    @Override
    public String readSD() {
        String msg = "sd card readTF: hello!";
        return msg;
    }

    @Override
    public void writeSD(String msg) {
        System.out.println("sd card writeTF: " + msg);
    }

}
/**
 * 适配器:SD 兼容 TF
 */
public class SDAdapterTF 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 {

    public String readSD(SDCard sdCard) {
        if(sdCard == null) {
            throw new NullPointerException("sd card null");
        }
        return sdCard.readSD();
    }

}
    public static void main(String[] args) {
        //1.创建一个电脑类
        Computer computer = new Computer();
        //3.通过适配器从电脑中读取 TF 卡的数据
        SDAdapterTF adapter = new SDAdapterTF();
        String msg = computer.readSD(adapter);
        System.out.println(msg);
    }

1.2.2、对象适配器

对象适配器,相比于 类适配器,更符合 合成/聚合复用原则(持有新对象的引用,而不是通过继承来达到复用目的).  也就是说,它是通过持有目标接口的引用(tf 卡接口的引用),重写 适配者接口 的所有方法实现的 .  

/**
 * 目标接口: TF 卡
 */
public interface TFCard {

    /**
     * 读取 TF 卡
     * @return
     */
    String readTF();

    /**
     * 写入 TF 卡
     */
    void writeTF(String msg);

}
/**
 * 目标接口实现类
 */
public class TFCardImpl implements TFCard {

    @Override
    public String readTF() {
        String msg = "tf card readTF: hello!";
        return msg;
    }

    @Override
    public void writeTF(String msg) {
        System.out.println("tf card writeTF: hello!");
    }

}

/**
 * 适配者接口: SD 卡
 */
public interface SDCard {

    /**
     * 读取 SD 卡
     * @return
     */
    String readSD();

    /**
     * 写入 SD 卡
     */
    void writeSD(String msg);

}
/**
 * 适配者实现类: SD 卡实现类
 */
public class SDCardImpl implements SDCard {

    @Override
    public String readSD() {
        String msg = "sd card readTF: hello!";
        return msg;
    }

    @Override
    public void writeSD(String msg) {
        System.out.println("sd card writeTF: " + msg);
    }

}
/**
 * 适配器:SD 兼容 TF
 */
public class SDAdapterTF implements SDCard {

    private TFCard tfCard;

    public SDAdapterTF(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 {

    public String readSD(SDCard sdCard) {
        if(sdCard == null) {
            throw new NullPointerException("sd card null");
        }
        return sdCard.readSD();
    }

}
public class Client {

    public static void main(String[] args) {
        //1.创建一个电脑类
        Computer computer = new Computer();
        //3.通过适配器从电脑中读取 TF 卡的数据
        SDAdapterTF adapter = new SDAdapterTF(new TFCardImpl());
        computer.readSD(adapter);
    }

}

1.2.3、接口适配器

当我们不希望实现一个适配者接口(sd 卡接口)中的所有方法时,可以创建一个抽象类 Adapter,实现所有方法(不用实现方法内容).此时我们只需要继承该抽象类,在重写我们需要的方法即可.

实现前两个适配器中,就一直没有使用 writeSD 方法,因此这里就不实现此方法.

/**
 * 目标接口: TF 卡
 */
public interface TFCard {

    /**
     * 读取 TF 卡
     * @return
     */
    String readTF();

    /**
     * 写入 TF 卡
     */
    void writeTF(String msg);

}
/**
 * 目标接口实现类
 */
public class TFCardImpl implements TFCard {

    @Override
    public String readTF() {
        String msg = "tf card readTF: hello!";
        return msg;
    }

    @Override
    public void writeTF(String msg) {
        System.out.println("tf card writeTF: hello!");
    }

}
/**
 * 适配者接口: SD 卡
 */
public interface SDCard {

    /**
     * 读取 SD 卡
     * @return
     */
    String readSD();

    /**
     * 写入 SD 卡
     */
    void writeSD(String msg);

}
/**
 * 适配者实现类: SD 卡实现类
 */
public class SDCardImpl implements SDCard {

    @Override
    public String readSD() {
        String msg = "sd card readTF: hello!";
        return msg;
    }

    @Override
    public void writeSD(String msg) {
        System.out.println("sd card writeTF: " + msg);
    }

}
public abstract class Adapter implements SDCard {


    @Override
    public void writeSD(String msg) {
    }

    @Override
    public String readSD() {
        return null;
    }

}
public class SDAdapterTF extends Adapter implements SDCard{

    private TFCard tfCard;

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

    @Override
    public String readSD() {
        System.out.println("adapter read tf card");
        return tfCard.readTF();
    }
}
/**
 * 电脑类
 */
public class Computer {

    public String readSD(SDCard sdCard) {
        if(sdCard == null) {
            throw new NullPointerException("sd card null");
        }
        return sdCard.readSD();
    }

}
public class Client {

    public static void main(String[] args) {
        //1.创建一个电脑类
        Computer computer = new Computer();
        //2.通过适配器从电脑中读取 TF 卡的数据
        SDAdapterTF sdAdapterTF = new SDAdapterTF(new TFCardImpl());
        String msg = computer.readSD(sdAdapterTF);
        System.out.println(msg);
    }

}

1.3、优缺点(对象适配器模式)

优点

1. 适配现有类,且不修改类:在不改变现有类的基础上,实现现有类和目标类的接口的匹配.

2. 符合 合成/聚合 复用原则:持有引用,而不继承.

3. 符合开闭原则:如果引入新的目标接口,只需要在适配器类中进行扩展,不需要修改原代码.

缺点:

增加复杂性:编写适配器类时,要考虑全面,包括适配者和目标类.

1.4、应用场景

1. 以前开发的系统中存在满足当前业务所需要的类,但是接口和当前业务所需接口不一致.

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

【版权声明】本文内容来自摩杜云社区用户原创、第三方投稿、转载,内容版权归原作者所有。本网站的目的在于传递更多信息,不拥有版权,亦不承担相应法律责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@moduyun.com

  1. 分享:
最后一次编辑于 2024年03月30日 0

暂无评论

TEZNKK3IfmPf