Created
May 29, 2013 17:54
-
-
Save danveloper/5672279 to your computer and use it in GitHub Desktop.
Introduction - "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 Payee { | |
String name | |
String phoneNumber | |
Address billingAddress | |
} | |
public interface PaymentType { | |
Payee getPayee(); | |
} | |
class CreditCardPaymentType implements PaymentType { | |
enum CreditCardType { | |
VISA, MASTERCARD, AMEX, DISCOVER | |
} | |
CreditCardType cardType | |
String cardNumber | |
String securityCode | |
Date expirationDate | |
Payee payee | |
} | |
class GiftCardPaymentType implements PaymentType { | |
String cardNumber | |
String securityCode | |
Payee payee | |
} |
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
public interface PaymentService { | |
/** | |
* Returns an authorization code | |
*/ | |
public String process(PaymentType paymentType); | |
} | |
class LocalPaymentService implements PaymentService { | |
// Dependency injected from container | |
CreditCardPaymentGateway creditCardPaymentGateway | |
GiftCardPaymentGateway giftCardPaymentGateway | |
public String process(PaymentType paymentType) throws IllegalArgumentException { | |
if (paymentType instanceof CreditCardPaymentType) { | |
return doProcess(paymentType) | |
} else if (paymentType instanceof GiftCardPaymentType) { | |
return doProcess(paymentType) | |
} | |
throw new IllegalArgumentException("Supplied payment type is not supported."); | |
} | |
private String doProcess(CreditCardPaymentType paymentType) { | |
// ... | |
} | |
private String doProcess(GiftCardPaymentType paymentType) { | |
// ... | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment