Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Dependency Injection in Apex
public interface PaymentInterface{
String processPayment();
}
public class PaypalPaymentService implements PaymentInterface{
public String processPayment(){
// 1. Connect to payment processor
// 2. ???
// 3. Profit!
return 'Paypal successfully processed your payment!';
}
}
public class ServiceFactory{
public static PaymentInterface getPaymentInterface(){
return (PaymentInterface)Type.forName(InheritenceSettings__c.getInstance().PaymentService__c).newInstance();
}
}
public class StripePaymentService implements PaymentInterface{
public String processPayment(){
// 1. Connect to payment processor
// 2. ???
// 3. Profit!
return 'Stripe successfully processed your payment!';
}
}
Owner

jessealtman commented Apr 19, 2014

This code is from my blog article Dependency Injection in Apex

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment