Add all 23 GoF design pattern implementations (2026-06-13)
This commit is contained in:
31
02-structural/adapter/OrderService.java
Normal file
31
02-structural/adapter/OrderService.java
Normal file
@@ -0,0 +1,31 @@
|
||||
package adapter;
|
||||
|
||||
/**
|
||||
* The Client — uses only the PaymentGateway interface.
|
||||
* It has no idea whether it's talking to Stripe, PayPal, or Braintree.
|
||||
* This is the point: the client is completely isolated from the vendor.
|
||||
*/
|
||||
public class OrderService {
|
||||
|
||||
private final PaymentGateway gateway;
|
||||
|
||||
// Receives a PaymentGateway — could be Stripe, PayPal, anything
|
||||
public OrderService(PaymentGateway gateway) {
|
||||
this.gateway = gateway;
|
||||
}
|
||||
|
||||
public void processOrder(String orderId, String customerId, double total) {
|
||||
System.out.printf("%nProcessing order %s for customer %s, total: $%.2f%n",
|
||||
orderId, customerId, total);
|
||||
|
||||
boolean charged = gateway.charge(customerId, total, "USD");
|
||||
|
||||
if (charged) {
|
||||
System.out.println("Payment accepted. Order confirmed.");
|
||||
String status = gateway.getStatus("ch_" + orderId);
|
||||
System.out.println("Transaction status: " + status);
|
||||
} else {
|
||||
System.out.println("Payment failed. Order rejected.");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user