Last active
December 17, 2015 21:09
-
-
Save danveloper/5672741 to your computer and use it in GitHub Desktop.
Redefined - "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 { | |
static { | |
use (PaymentTypeHandlerRegistrationCategory) { | |
CreditCardPaymentType.register(CreditCardPaymentProcessor) | |
GiftCardPaymentType.register(GiftCardPaymentProcessor) | |
EmployeePaymentType.register(EmployeePaymentProcessor) | |
CheckPaymentType.register(CheckPaymentProcessor) | |
} | |
} | |
public String process(PaymentType paymentType) { | |
def processorCategory | |
use (PaymentTypeHandlerRegistrationCategory) { | |
processorCategory = paymentType.class.getHandler() | |
} | |
// No processor for the payment type | |
if (!processorCategory) throw new RuntimeException("No handler registered for ${paymentType.class}") | |
use (processorCategory) { | |
paymentType.process() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment