当你希望使用某个类, 但是其接口与其他代码不兼容时, 可以使用适配器类。 通过适配器实现让一个只能读取SD卡的电脑可以读取TF卡
public interface SDCard {
void writeSD(String content);
String readSD();
}
public interface TFCard {
void writeTF(String content);
String readTF();
}
public class SDCardImpl implements SDCard{
@Override
public void writeSD(String content) {
System.out.println("SD card content:"+content);
}
@Override
public String readSD() {
return "SD card content";
}
}
public class TFCardImpl implements TFCard{
@Override
public void writeTF(String content) {
System.out.println("TF card content:"+content);
}
@Override
public String readTF() {
return "TF card content";
}
}
电脑只能读取SD卡,但是想要读取TF卡。
public class Computer {
public String readSD(SDCard sdCard) {
return sdCard.readSD();
}
public void writeSD(SDCard sdCard, String msg) {
sdCard.writeSD(msg);
}
}
添加一个适配器 类适配器
public class ClassAdapter extends TFCardImpl implements SDCard{
@Override
public void writeSD(String content) {
writeTF(content);
}
@Override
public String readSD() {
return readTF();
}
}
对象适配器
public class ObjectAdapter implements SDCard{
private TFCard tfCard;
@Override
public void writeSD(String content) {
tfCard.writeTF(content);
}
@Override
public String readSD() {
return tfCard.readTF();
}
}
分别使用对象适配器和类适配器实现
public class Client {
public static void main(String[] args) {
Computer computer=new Computer();
//使用类适配器
// ClassAdapter classAdapter = new ClassAdapter();
// classAdapter(computer, classAdapter);
//使用对象适配器
ObjectAdapter objectAdapter = new ObjectAdapter(new TFCardImpl());
objectAdapter(computer, objectAdapter);
}
private static void objectAdapter(Computer computer, ObjectAdapter objectAdapter) {
computer.writeSD(objectAdapter,"使用对象适配器将内容写入到TF卡");
String tfContent = computer.readSD(objectAdapter);
System.out.println(tfContent);
}
private static void classAdapter(Computer computer, ClassAdapter classAdapter) {
computer.writeSD(classAdapter,"使用类适配器将内容写到TF卡");
String tfContent = computer.readSD(classAdapter);
System.out.println(tfContent);
}
}
当前系统只支持支付宝支付,现添加一个适配器使其支持微信支付
public class Client {
public static void main(String[] args) {
Payment alipay = new Alipay();
alipay.pay(100.0); // 现有支付宝支付
// 你的适配器应支持如下调用:
Payment wechatPay = new ClassAdapter();
wechatPay.pay(200.0); // 通过适配器调用微信支付
}
}
//对象适配器
class ObjectAdapter implements Payment{
private WeChatPay weChatPay;
public ObjectAdapter(WeChatPay weChatPay) {
this.weChatPay = weChatPay;
}
@Override
public void pay(double amount) {
weChatPay.weChatPay(amount);
}
}
//类适配器
class ClassAdapter extends WeChatPay implements Payment{
@Override
public void pay(double amount) {
weChatPay(amount);
}
}
// 目标接口:系统当前统一的支付接口
interface Payment {
void pay(double amount);
}
// 支付宝支付(已实现的适配者)
class Alipay implements Payment {
@Override
public void pay(double amount) {
System.out.println("支付宝支付: " + amount + "元");
}
}
// 微信支付(需要适配的第三方类,接口不兼容)
class WeChatPay {
public void weChatPay(double money) {
System.out.println("微信支付: " + money + "元");
}
}
-
优先对象适配器(推荐):
- 更灵活,避免继承的局限性。
- 符合“组合优于继承”原则。
-
类适配器 仅当以下条件满足:
- 需要重写被适配者的方法。
- 目标接口和被适配者类的方法高度匹配。
- 无多继承冲突(如目标接口是纯接口)