Skip to content

Instantly share code, notes, and snippets.

@danveloper
danveloper / Service.groovy
Last active December 17, 2015 21:09
Redefined - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
static {
use (PaymentTypeHandlerRegistrationCategory) {
CreditCardPaymentType.register(CreditCardPaymentProcessor)
GiftCardPaymentType.register(GiftCardPaymentProcessor)
EmployeePaymentType.register(EmployeePaymentProcessor)
CheckPaymentType.register(CheckPaymentProcessor)
}
}
@danveloper
danveloper / Service.groovy
Created May 29, 2013 18:44
Registration - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
static {
use (PaymentTypeHandlerRegistrationCategory) {
CreditCardPaymentType.register(CreditCardPaymentProcessor)
GiftCardPaymentType.register(GiftCardPaymentProcessor)
EmployeePaymentType.register(EmployeePaymentProcessor)
CheckPaymentType.register(CheckPaymentProcessor)
}
}
@danveloper
danveloper / Registrar.groovy
Created May 29, 2013 18:40
Registrar - "Redefining the Service Layer with Groovy Categories"
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]
@danveloper
danveloper / CheckPaymentProcessor.groovy
Last active December 17, 2015 21:09
Bettering - "Redefining the Service Layer with Groovy Categories"
class CheckPaymentProcessor {
static String process(PaymentType paymentType) {
def gateway = new CheckPaymentProcessor()
// … implementation code ...
}
}
@danveloper
danveloper / Service.groovy
Created May 29, 2013 18:24
Redefining Further - "Redefining the Service Layer with Groovy Categories"
class LocalPaymentService implements PaymentService {
public String process(PaymentType paymentType) {
if (paymentType instanceof CreditCardPaymentType) {
use (CreditCardPaymentProcessor) {
return paymentType.process()
}
}
// ...
}
@danveloper
danveloper / Processor.groovy
Last active December 17, 2015 21:09
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()
@danveloper
danveloper / Service.groovy
Last active December 17, 2015 21:09
Obscurity - "Redefining the Service Layer with Groovy Categories"
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) {
@danveloper
danveloper / Service.groovy
Created May 29, 2013 18:12
Fragility - "Redefining the Service Layer with Groovy Categories"
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) {
@danveloper
danveloper / Service.groovy
Created May 29, 2013 18:08
Expansion - "Redefining the Service Layer with Groovy Categories"
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) {
@danveloper
danveloper / DataModel.groovy
Created May 29, 2013 17:54
Introduction - "Redefining the Service Layer with Groovy Categories"
class Payee {
String name
String phoneNumber
Address billingAddress
}
public interface PaymentType {
Payee getPayee();
}