parent
70ed948e92
commit
cf623dbc80
@ -0,0 +1,33 @@
|
||||
// 统一支付接口
|
||||
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); // 统一接口调用
|
||||
}
|
||||
}
|
||||
Loading…
Reference in new issue