Last active
December 17, 2015 21:09
-
-
Save danveloper/5672473 to your computer and use it in GitHub Desktop.
Obscurity - "Redefining the Service Layer with Groovy Categories"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LocalPaymentService implements PaymentService { | |
// Dependency injected from container | |
CreditCardPaymentGateway creditCardPaymentGateway | |
GiftCardPaymentGateway giftCardPaymentGateway | |
CheckPaymentGateway checkPaymentGateway | |
EmployeePaymentGateway employeePaymentGateway | |
public String process(PaymentType paymentType) throws IllegalArgumentException { | |
if (paymentType instanceof CreditCardPaymentType) { | |
return doProcess(paymentType) | |
} else if (paymentType instanceof GiftCardPaymentType) { | |
return doProcess(paymentType) | |
} else if (paymentType instanceof CheckPaymentType) { | |
return doProcess(paymentType) | |
} else if (paymentType instanceof EmployeePaymentType) { | |
return doProcess(paymentType) | |
} | |
throw new IllegalArgumentException("Supplied payment type is not supported."); | |
} | |
private String doProcess(CreditCardPaymentType paymentType) { | |
if ("CA".equalsIgnoreCase(paymentType.getPayee().getBillingAddress().getState())) { | |
return doProcessCalifornia(paymentType) | |
} | |
return doProcessOther(paymentType) | |
} | |
private String doProcessCalifornia(CreditCardPaymentType paymentType) { | |
// ... | |
} | |
private String doProcessOther(CreditCardPaymentType paymentType) { | |
// … | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment