Home  >  Article  >  Java  >  Two categories of Java adapter patterns and specific application scenarios

Two categories of Java adapter patterns and specific application scenarios

PHPz
PHPzforward
2023-04-22 09:31:071439browse

1. Class adapter pattern

Implementation method: Define an adapter class to implement the business interface of the current system, and at the same time inherit the components that already exist in the existing component library.

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. Object Adapter Pattern

Implementation method: The object adapter pattern can introduce components that have been implemented in the existing component library into the adapter, and the class can be implemented at the same time The business interface of the current system.

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);
    }
}

The above is the detailed content of Two categories of Java adapter patterns and specific application scenarios. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete