Advertisement
Guest User

Untitled

a guest
Jun 25th, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. public with sharing class ClassFactory
  2. {
  3. // Class Factory template
  4. public interface IClassFactory
  5. {
  6. void processWork();
  7. }
  8.  
  9. // Class Factory base class
  10. public virtual class ClassFactoryBase
  11. {
  12. // ... Shared methods go here
  13. }
  14.  
  15.  
  16. // Process work
  17. public static void processAllWork()
  18. {
  19. ClassFactoryManager cfm = new ClassFactoryManager();
  20. cfm.newClassInstance('ClassFactory.Class1').processWork();
  21. cfm.newClassInstance('ClassFactory.Class2').processWork();
  22. cfm.newClassInstance('ClassFactory.Class3').processWork();
  23. cfm.newClassInstance('ClassFactory.Class4').processWork();
  24. }
  25.  
  26. // Class1
  27. public class Class1 extends ClassFactoryBase implements IClassFactory
  28. {
  29. public void processWork()
  30. {
  31. // ... Class-specific work goes here
  32. }
  33. }
  34.  
  35. // Class2
  36. public class Class2 extends ClassFactoryBase implements IClassFactory
  37. {
  38. public void processWork()
  39. {
  40. // ... Class-specific work goes here
  41. }
  42. }
  43.  
  44. // Class3
  45. public class Class3 extends ClassFactoryBase implements IClassFactory
  46. {
  47. public void processWork()
  48. {
  49. // ... Class-specific work goes here
  50. }
  51. }
  52.  
  53. // Class4
  54. public class Class4 extends ClassFactoryBase implements IClassFactory
  55. {
  56. public void processWork()
  57. {
  58. // ... Class-specific work goes here
  59. }
  60. }
  61. }
  62.  
  63. public with sharing class ClassFactoryManager
  64. {
  65. public ClassFactoryManager(){}
  66.  
  67. // Return the appropriate class instance based on className
  68. public ClassFactory.IClassFactory newClassInstance(String className)
  69. {
  70. Type t = Type.forName(className);
  71. return (ClassFactory.IClassFactory) t.newInstance();
  72. }
  73. }
  74.  
  75. > ToolingAPI x = new ToolingAPI(); ToolingAPI.ExecuteAnonymousResult
  76. > toolingResult = x.executeAnonymousUnencoded("Your apex code as a
  77. > string here");
  78.  
  79. public class Extension implements Callable {
  80.  
  81. // Actual method
  82. String concatStrings(String stringValue) {
  83. return stringValue + stringValue;
  84. }
  85.  
  86. // Actual method
  87. Decimal multiplyNumbers(Decimal decimalValue) {
  88. return decimalValue * decimalValue;
  89. }
  90.  
  91. // Dispatch actual methods
  92. public Object call(String action, Map<String, Object> args) {
  93. switch on action {
  94. when 'concatStrings' {
  95. return this.concatStrings((String)args.get('stringValue'));
  96. }
  97. when 'multiplyNumbers' {
  98. return this.multiplyNumbers((Decimal)args.get('decimalValue'));
  99. }
  100. when else {
  101. throw new ExtensionMalformedCallException('Method not implemented');
  102. }
  103. }
  104. }
  105.  
  106. public class ExtensionMalformedCallException extends Exception {}
  107. }
  108.  
  109. @IsTest
  110. private with sharing class ExtensionCaller {
  111.  
  112. @IsTest
  113. private static void givenConfiguredExtensionWhenCalledThenValidResult() {
  114.  
  115. // Given
  116. String className = 'Extension'; // Variable to demonstrate setting class name
  117. String methodName = 'multiplyNumbers'; // Variable to demonstrate setting method name
  118. Decimal decimalTestValue = 10;
  119.  
  120. // When
  121. Callable extension = (Callable) Type.forName(className).newInstance();
  122. Decimal result = (Decimal) extension.call(methodName, new Map<String, Object> { 'decimalValue' => decimalTestValue });
  123.  
  124. // Then
  125. System.assertEquals(100, result);
  126. }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement