【设计模式|结构型】适配器模式(Adapter Pattern)

背景

假设你有一个美国制造的电子设备,它的插头是美国标准的两脚插头(Type A插头),而你想在中国使用它,但中国的电源插座是中国标准的三脚插座(Type I插座)。这两种插头标准是不兼容的,无法直接连接。

这时,你需要一个适配器来解决这个问题。

概述

适配器模式是一种结构型设计模式,它可以将一个类的接口转换成另一个客户端所期望的接口。适配器模式旨在解决接口不兼容的问题,它包括目标接口(Target)、适配器(Adapter)和适配者(Adaptee)三个角色。

再举几个例子:

电源适配器、蓝牙适配器、翻译工具、视频转换器、音频适配器

示例一

以美国电子设备在中国使用为例:

定义一个中国标准的插座接口:

public interface ChinaSocket {
    void powerSupply();
}

定义一个美国标准的插头接口:

public interface USPlug {
    void plugIn();
}

实现中国标准的插座接口:

public class ChinaSocketImpl implements ChinaSocket {
    @Override
    public void powerSupply() {
        System.out.println("中国标准的插座供电");
    }
}

实现美国标准的插头接口:

public class USPlugImpl implements USPlug {
    @Override
    public void plugIn() {
        System.out.println("美国标准的插头插入");
    }
}

创建一个适配器类,实现中国标准的插座接口,并在内部包含一个美国标准的插头实例:

public class USPlugAdapter implements ChinaSocket {
    private USPlug usPlug;

    public USPlugAdapter(USPlug usPlug) {
        this.usPlug = usPlug;
    }

    @Override
    public void powerSupply() {
        System.out.println("适配器将美国标准的插头转换为中国标准的插座");
        usPlug.plugIn();
    }
}

测试:

public class Main {
    public static void main(String[] args) {
        // 创建一个中国标准的插座
        ChinaSocket chinaSocket = new ChinaSocketImpl();
        
        // 创建一个美国标准的插头
        USPlug usPlug = new USPlugImpl();
        
        // 创建一个适配器,将美国标准的插头转换为中国标准的插座
        ChinaSocket adapter = new USPlugAdapter(usPlug);
        
        // 使用适配器进行供电
        adapter.powerSupply();
    }
}

运行上述代码,输出结果为

适配器将美国标准的插头转换为中国标准的插座

美国标准的插头插入

示例二

应用场景: 在使用第三方支付平台时,支付平台提供的支付接口与自己系统内部的支付接口通常是不一致的。这时候,可以使用适配器模式来实现对支付接口的适配。例如,当系统需要使用第三方支付平台进行支付时,系统会通过适配器来调用相应的支付接口进行支付操作。

目标接口,定义了系统内部所支持的支付接口:

public interface Payment {
    void pay(String paymentType, double amount);
}

适配者接口,定义了第三方支付平台所提供的支付接口:

public interface ThirdPartyPayment {
    void doPayment(double amount);
}

适配器,将第三方支付平台的支付接口适配成系统内部所支持的支付接口:

public class PaymentAdapter implements Payment {
    private ThirdPartyPayment thirdPartyPayment;

    public PaymentAdapter(ThirdPartyPayment thirdPartyPayment) {
        this.thirdPartyPayment = thirdPartyPayment;
    }

    @Override
    public void pay(String paymentType, double amount) {
        if(paymentType.equalsIgnoreCase("thirdPartyPayment")) {
            thirdPartyPayment.doPayment(amount);
        } else {
            System.out.println("Invalid payment type. Only thirdPartyPayment is supported.");
        }
    }
}

实现适配者接口的类,此处以支付宝为例:

public class AliPay implements ThirdPartyPayment {
    @Override
    public void doPayment(double amount) {
        System.out.println("Making payment using AliPay: " + amount);
    }
}

实现适配者接口的类,此处以微信支付为例:

public class WeChatPay implements ThirdPartyPayment {
    @Override
    public void doPayment(double amount) {
        System.out.println("Making payment using WeChatPay: " + amount);
    }
}

客户端代码,通过适配器使用第三方支付平台进行支付:

public class Main {
    public static void main(String[] args) {
        Payment payment = new PaymentAdapter(new AliPay());

        payment.pay("thirdPartyPayment", 100.0);

        Payment payment2 = new PaymentAdapter(new WeChatPay());

        payment2.pay("thirdPartyPayment", 200.0);

        Payment payment3 = new PaymentAdapter(new AliPay());

        payment3.pay("wrongPaymentType", 300.0);
    }
}

运行上述代码,输出结果为

Making payment using AliPay: 100.0
Making payment using WeChatPay: 200.0
Invalid payment type. Only thirdPartyPayment is supported. 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值