Created
April 19, 2014 14:04
-
-
Save jessealtman/11085277 to your computer and use it in GitHub Desktop.
Dependency Injection in Apex
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
public interface PaymentInterface{ | |
String processPayment(); | |
} |
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
public class PaypalPaymentService implements PaymentInterface{ | |
public String processPayment(){ | |
// 1. Connect to payment processor | |
// 2. ??? | |
// 3. Profit! | |
return 'Paypal successfully processed your payment!'; | |
} | |
} |
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
public class ServiceFactory{ | |
public static PaymentInterface getPaymentInterface(){ | |
return (PaymentInterface)Type.forName(InheritenceSettings__c.getInstance().PaymentService__c).newInstance(); | |
} | |
} |
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
public class StripePaymentService implements PaymentInterface{ | |
public String processPayment(){ | |
// 1. Connect to payment processor | |
// 2. ??? | |
// 3. Profit! | |
return 'Stripe successfully processed your payment!'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This code is from my blog article Dependency Injection in Apex