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) | |
} | |
} |
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) | |
} | |
} |
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 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 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 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 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 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 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 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 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 Payee { | |
String name | |
String phoneNumber | |
Address billingAddress | |
} | |
public interface PaymentType { | |
Payee getPayee(); | |
} |