适配器设计模式

1.类适配器模式

java 复制代码
package com.jmj.pattern.adapter.class_adapter;

/**
 * 目标接口
 */
public interface SDCard {

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



}
java 复制代码
package com.jmj.pattern.adapter.class_adapter;

/**
 * 具体的SD卡类
 */
public class SDCardImpl implements SDCard{
    @Override
    public String readSD() {
        String msg="SDCard read msg : hello word SD";
        return msg;
    }

    @Override
    public void writeSD(String msg) {
        System.out.println("SDCard write msg :" + msg);
    }
}
java 复制代码
package com.jmj.pattern.adapter.class_adapter;

/**
 * 适配者类的接口
 */
public interface TFCard {

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



}
java 复制代码
package com.jmj.pattern.adapter.class_adapter;

/**
 * 适配者类
 */
public class TFCardImpl implements TFCard{
    @Override
    public String readTF() {
        return "TFCard read msg: hello word TFCard";
    }

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


}
java 复制代码
package com.jmj.pattern.adapter.class_adapter;

/**
 * 计算机类
 */
public class Computer {


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






}
java 复制代码
package com.jmj.pattern.adapter.class_adapter;

/**
 * 适配器类
 */
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 wirte tf card");
        writeTF(msg);
    }
}
java 复制代码
package com.jmj.pattern.adapter.class_adapter;

public class Client {
    public static void main(String[] args) {
        Computer computer = new Computer();
        String s = computer.readSD(new SDCardImpl());
        System.out.println(s);

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

    }
}

2.对象适配器模式

java 复制代码
package com.jmj.pattern.adapter.object_adapter;

import com.jmj.pattern.adapter.class_adapter.TFCardImpl;

/**
 * 适配器类
 */
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 wirte tf card");
        tfCard.writeTF(msg);
    }
}

相关推荐
csbysj202024 分钟前
DOM 节点
开发语言
用户849137175471629 分钟前
ThreadLocal 源码深度解析:JDK 设计者的“妥协”与“智慧”
java·后端
用户03048059126332 分钟前
# 【Maven避坑】源码去哪了?一文看懂 Maven 工程与打包后的目录映射关系
java·后端
小尧嵌入式1 小时前
C++基础语法总结
开发语言·c++·stm32·单片机·嵌入式硬件·算法
@游子1 小时前
Python学习笔记-Day2
开发语言·python
v***55341 小时前
springboot使用logback自定义日志
java·spring boot·logback
qq_336313931 小时前
java基础-集合进阶
java·开发语言·windows
稚辉君.MCA_P8_Java1 小时前
Gemini永久会员 归并排序(Merge Sort) 基于分治思想(Divide and Conquer)的高效排序算法
java·linux·算法·spring·排序算法
q***18841 小时前
Spring Boot中的404错误:原因、影响及处理策略
java·spring boot·后端
222you1 小时前
MybatisPlus常用注解
java·开发语言·spring