Guest User

Untitled

a guest
Jun 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. global class FooProvider {
  2. //V1
  3. global interface CustomFooProviderV1 {
  4. string bar();
  5. }
  6.  
  7. public static CustomFooProviderV1 getFooProviderInstance() {
  8. // Use a CMDT or Custom Setting to get the name of the implementing class
  9. // Use Type.forName and Type.newInstance() to create instance
  10. //...
  11. return (CustomFooProviderV1)fooInstance;
  12. }
  13. }
  14.  
  15. global class FooProvider {
  16. //V1
  17. global interface CustomFooProviderV1 {
  18. string bar();
  19. }
  20.  
  21. //V2
  22. global interface CustomFooProviderV2 extends CustomFooProviderV1 {
  23. string raiseTheBar(int height);
  24. }
  25.  
  26. public static CustomFooProviderV2 getFooProviderInstance() {
  27. // Use a CMDT or Custom Setting to get the name of the implementing class
  28. string className = 'localFooProvider';
  29. // Use Type.forName and Type.newInstance() to create instance
  30. //...
  31. Type t = Type.forName(className);
  32.  
  33. // Create an instance to confirm the type
  34. object testInstance = t.newInstance();
  35. if(testInstance instanceOf CustomFooProviderV2) {
  36. return testInstance;
  37. }
  38. if(testInstance instanceOf CustomFooProviderV1) {
  39. return testInstance;
  40. }
  41.  
  42. return (CustomFooProviderV1)fooInstance;
  43. }
  44.  
  45. public class FooUpgrader implements CustomFooProviderV2 {
  46. private CustomFooProviderV1 v1;
  47. public FooUpgrader(CustomFooProviderV1 v1Param) {
  48. v1 = v1Param;
  49. }
  50. public string bar() {
  51. return 'foobar';
  52. }
  53. public string raiseTheBar(int height) {
  54. throw new InterfaceVersionException('Method raiseTheBar is not supported with an implementation of the v1 FooProvider interface');
  55. }
  56. }
  57. public class InterfaceVersionException extends Exception {}
  58. }
Add Comment
Please, Sign In to add comment