Last active
December 17, 2015 21:09
-
-
Save danveloper/5672499 to your computer and use it in GitHub Desktop.
Redefining - "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 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