java适配器模式的两种分类

2026-01-29 0 58,586

1、类适配器模式

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

public class HelloWorld {
    public static void main(String[] args) {
        Computer computer = new Computer();
        SDCard sdCard = new SDCardImpl();
        System.out.println(computer.readSD(sdCard));
 
        System.out.println("------------");
 
        SDAdapterTF adapter = new SDAdapterTF();
        System.out.println(computer.readSD(adapter));
    }
}
 
// SD卡的接口
interface SDCard {
    // 读取SD卡功能
    String readSD();
 
    // 写入SD卡功能
    void writeSD(String msg);
}
 
// SD卡实现类
class SDCardImpl implements SDCard {
    @Override
    public String readSD() {
        String msg = "sd card read a msg: hello sd card";
        return msg;
    }
 
    @Override
    public void writeSD(String msg) {
        System.out.println("sd card write msg: " + msg);
    }
}
 
// 电脑类
class Computer {
    public String readSD(SDCard sdCard) {
        if (sdCard == null) {
            throw new NullPointerException("sd card null");
        }
        return sdCard.readSD();
    }
}
 
// TF卡接口
interface TFCard {
    // 读取TF卡功能
    String readTF();
 
    // 写入TF卡功能
    void writeTF(String msg);
}
 
// TF卡实现类
class TFCardImpl implements TFCard {
    @Override
    public String readTF() {
        String msg = "sd card read a msg: hello tf card";
        return msg;
    }
 
    @Override
    public void writeTF(String msg) {
        System.out.println("tf card write msg: " + msg);
    }
}
 
// 定义适配器类(SD兼容TF)
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);
    }
}

2、对象适配器模式

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

public class HelloWorld {
    public static void main(String[] args) {
        Computer computer = new Computer();
        SDCard sdCard = new SDCardImpl();
        System.out.println(computer.readSD(sdCard));
 
        System.out.println("------------");
        
        TFCard tfCard = new TFCardImpl();
        SDAdapterTF adapter = new SDAdapterTF(tfCard);
        System.out.println(computer.readSD(adapter));
    }
}
 
// SD卡的接口
interface SDCard {
    // 读取SD卡功能
    String readSD();
 
    // 写入SD卡功能
    void writeSD(String msg);
}
 
// SD卡实现类
class SDCardImpl implements SDCard {
    @Override
    public String readSD() {
        String msg = "sd card read a msg: hello sd card";
        return msg;
    }
 
    @Override
    public void writeSD(String msg) {
        System.out.println("sd card write msg: " + msg);
    }
}
 
// 电脑类
class Computer {
    public String readSD(SDCard sdCard) {
        if (sdCard == null) {
            throw new NullPointerException("sd card null");
        }
        return sdCard.readSD();
    }
}
 
// TF卡接口
interface TFCard {
    // 读取TF卡功能
    String readTF();
 
    // 写入TF卡功能
    void writeTF(String msg);
}
 
// TF卡实现类
class TFCardImpl implements TFCard {
    @Override
    public String readTF() {
        String msg = "sd card read a msg: hello tf card";
        return msg;
    }
 
    @Override
    public void writeTF(String msg) {
        System.out.println("tf card write msg: " + msg);
    }
}
 
// 定义适配器类(SD兼容TF)
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);
    }
}

以上就是java适配器模式的两种分类,希望能对大家有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:以上部本文内容由互联网用户自发贡献,本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。投诉邮箱:3758217903@qq.com

ZhiUp资源网 java教程 java适配器模式的两种分类 https://www.zhiup.top/10929.html

相关