Files
design-patterns/02-structural/adapter/StripeClient.java

52 lines
1.9 KiB
Java

package adapter;
/**
* The Adaptee — a third-party payment SDK with a completely different interface.
* Imagine this is Stripe's actual SDK: you cannot modify this class,
* and it doesn't implement PaymentGateway.
*
* In real projects, this would be a JAR you depend on.
*/
public class StripeClient {
private final String apiKey;
public StripeClient(String apiKey) {
this.apiKey = apiKey;
System.out.println("[Stripe] Initialized with key: " + apiKey.substring(0, 8) + "...");
}
// Stripe uses cents, not decimal amounts
public StripeChargeResult createCharge(String customerId, long amountInCents, String currency) {
System.out.printf("[Stripe] Charging customer=%s, amount=%d cents, currency=%s%n",
customerId, amountInCents, currency);
// Simulate success
return new StripeChargeResult("ch_" + System.currentTimeMillis(), true, null);
}
// Stripe's refund method takes a charge ID and uses different naming
public boolean issueRefund(String chargeId, long amountInCents) {
System.out.printf("[Stripe] Refunding charge=%s, amount=%d cents%n", chargeId, amountInCents);
return true;
}
// Stripe uses 'retrieve' not 'getStatus', and returns an object
public StripeChargeResult retrieveCharge(String chargeId) {
System.out.printf("[Stripe] Retrieving charge=%s%n", chargeId);
return new StripeChargeResult(chargeId, true, "succeeded");
}
// Stripe-specific result object — nothing in common with your domain
public static class StripeChargeResult {
public final String chargeId;
public final boolean success;
public final String status;
public StripeChargeResult(String chargeId, boolean success, String status) {
this.chargeId = chargeId;
this.success = success;
this.status = status;
}
}
}