Skip to content

Instantly share code, notes, and snippets.

@jessealtman
Created April 19, 2014 14:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jessealtman/11085277 to your computer and use it in GitHub Desktop.
Save jessealtman/11085277 to your computer and use it in GitHub Desktop.
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!';
}
}
@jessealtman
Copy link
Author

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