Skip to content

Instantly share code, notes, and snippets.

@danveloper
Last active December 17, 2015 21:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danveloper/5672499 to your computer and use it in GitHub Desktop.
Save danveloper/5672499 to your computer and use it in GitHub Desktop.
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