This file contains hidden or 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) { |
This file contains hidden or 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() |
This file contains hidden or 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 { | |
| public String process(PaymentType paymentType) { | |
| if (paymentType instanceof CreditCardPaymentType) { | |
| use (CreditCardPaymentProcessor) { | |
| return paymentType.process() | |
| } | |
| } | |
| // ... | |
| } |
This file contains hidden or 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 CheckPaymentProcessor { | |
| static String process(PaymentType paymentType) { | |
| def gateway = new CheckPaymentProcessor() | |
| // … implementation code ... | |
| } | |
| } |
This file contains hidden or 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 PaymentTypeHandlerRegistrationCategory { | |
| static final registry = [:] | |
| // type => handler registration | |
| static def register(Class type, Class handler) { | |
| registry[type] = handler | |
| } | |
| static def getHandler(Class type) { | |
| registry[type] |
This file contains hidden or 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 { | |
| static { | |
| use (PaymentTypeHandlerRegistrationCategory) { | |
| CreditCardPaymentType.register(CreditCardPaymentProcessor) | |
| GiftCardPaymentType.register(GiftCardPaymentProcessor) | |
| EmployeePaymentType.register(EmployeePaymentProcessor) | |
| CheckPaymentType.register(CheckPaymentProcessor) | |
| } | |
| } |
This file contains hidden or 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 { | |
| static { | |
| use (PaymentTypeHandlerRegistrationCategory) { | |
| CreditCardPaymentType.register(CreditCardPaymentProcessor) | |
| GiftCardPaymentType.register(GiftCardPaymentProcessor) | |
| EmployeePaymentType.register(EmployeePaymentProcessor) | |
| CheckPaymentType.register(CheckPaymentProcessor) | |
| } | |
| } |
This file contains hidden or 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
| package com.objectpartners.groovy.ext; | |
| import groovy.lang.MetaClass; | |
| import groovy.lang.MetaClassRegistry; | |
| import org.codehaus.groovy.runtime.GeneratedClosure; | |
| import org.codehaus.groovy.runtime.metaclass.ClosureMetaClass; | |
| class CustomMetaClassCreationHandle extends MetaClassRegistry.MetaClassCreationHandle { | |
| protected MetaClass createNormalMetaClass(Class theClass,MetaClassRegistry registry) { |
This file contains hidden or 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
| package com.objectpartners.groovy.ext; | |
| // imports... | |
| class MixinResolvingMetaClass extends MetaClassImpl { | |
| static final Map<Class, Class> cachedMixins = new ConcurrentHashMap<>(); | |
| MixinResolvingMetaClass(MetaClassRegistry registry, Class theClass) { | |
| super(registry, theClass); | |
| } |
This file contains hidden or 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
| package groovy.runtime.metaclass.java.lang | |
| import groovy.json.JsonSlurper | |
| class StringMixins { | |
| // Must be static & must take an instance of the receiver as the first vararg | |
| static def toJson(String input) { | |
| new JsonSlurper().parseText(input) | |
| } |