Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Redefining - "Redefining the Service Layer with Groovy Categories"
class CreditCardPaymentProcessor {
static {
CreditCardPaymentProcessor.metaClass.static.methodMissing = { String name, args ->
if (name.startsWith("getGatewayFor")) {
def state = name.replaceAll("getGatewayFor","")?.toLowerCase()
switch (state) {
case "ca":
return new CheckPaymentProcessor()
default:
return new CreditCardPaymentProcessor()
}
}
}
}
static String process(PaymentType paymentType) {
def gateway = "getGatewayFor${paymentType.payee.billingAddress.state}"()
process gateway, paymentType
}
static String process(CheckPaymentGateway paymentGateway, PaymentType paymentType) {
// ...
}
static String process(CreditCardPaymentGateway paymentGateway, PaymentType paymentType) {
//
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.