You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
trae1/BasicPaymentAdapter.java

33 lines
871 B

// 统一支付接口
interface PaymentGateway {
void pay(double amount);
}
// 旧版支付系统(需适配)
class LegacyPaySystem {
void executeLegacyPayment(String currency, double value) {
System.out.println("Processing legacy payment: " + value + " " + currency);
}
}
// 适配器类
class PaymentAdapter implements PaymentGateway {
private LegacyPaySystem legacySystem;
public PaymentAdapter(LegacyPaySystem system) {
this.legacySystem = system;
}
@Override
public void pay(double amount) {
legacySystem.executeLegacyPayment("USD", amount); // 硬编码货币为USD
}
}
// 客户端使用
public class BasicPaymentAdapter {
public static void main(String[] args) {
PaymentGateway gateway = new PaymentAdapter(new LegacyPaySystem());
gateway.pay(199.99); // 统一接口调用
}
}