Add all 23 GoF design pattern implementations
This commit is contained in:
36
02-structural/adapter/StripePaymentAdapter.java
Normal file
36
02-structural/adapter/StripePaymentAdapter.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package adapter;
|
||||
/**
|
||||
* The Adapter — bridges StripeClient to PaymentGateway.
|
||||
*
|
||||
* Object Adapter variant: holds a StripeClient instance via composition.
|
||||
* This means it can wrap any StripeClient including subclasses.
|
||||
*/
|
||||
public class StripePaymentAdapter implements PaymentGateway {
|
||||
private final StripeClient stripe;
|
||||
public StripePaymentAdapter(StripeClient stripe) {
|
||||
this.stripe = stripe;
|
||||
}
|
||||
@Override
|
||||
public boolean charge(String customerId, double amount, String currency) {
|
||||
// Translation 1: dollars → cents
|
||||
long amountInCents = Math.round(amount * 100);
|
||||
// Translation 2: charge() → createCharge(), lowercase currency
|
||||
StripeClient.StripeChargeResult result =
|
||||
stripe.createCharge(customerId, amountInCents, currency.toLowerCase());
|
||||
// Translation 3: StripeChargeResult → boolean
|
||||
return result.success;
|
||||
}
|
||||
@Override
|
||||
public boolean refund(String transactionId, double amount) {
|
||||
long amountInCents = Math.round(amount * 100);
|
||||
// Translation: refund() → issueRefund(), your "transactionId" is Stripe's "chargeId"
|
||||
return stripe.issueRefund(transactionId, amountInCents);
|
||||
}
|
||||
@Override
|
||||
public String getStatus(String transactionId) {
|
||||
// Translation: getStatus() → retrieveCharge(), StripeChargeResult → String
|
||||
StripeClient.StripeChargeResult result = stripe.retrieveCharge(transactionId);
|
||||
if (!result.success) return "FAILED";
|
||||
return result.status != null ? result.status.toUpperCase() : "UNKNOWN";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user