Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public with sharing class ClassFactory
- {
- // Class Factory template
- public interface IClassFactory
- {
- void processWork();
- }
- // Class Factory base class
- public virtual class ClassFactoryBase
- {
- // ... Shared methods go here
- }
- // Process work
- public static void processAllWork()
- {
- ClassFactoryManager cfm = new ClassFactoryManager();
- cfm.newClassInstance('ClassFactory.Class1').processWork();
- cfm.newClassInstance('ClassFactory.Class2').processWork();
- cfm.newClassInstance('ClassFactory.Class3').processWork();
- cfm.newClassInstance('ClassFactory.Class4').processWork();
- }
- // Class1
- public class Class1 extends ClassFactoryBase implements IClassFactory
- {
- public void processWork()
- {
- // ... Class-specific work goes here
- }
- }
- // Class2
- public class Class2 extends ClassFactoryBase implements IClassFactory
- {
- public void processWork()
- {
- // ... Class-specific work goes here
- }
- }
- // Class3
- public class Class3 extends ClassFactoryBase implements IClassFactory
- {
- public void processWork()
- {
- // ... Class-specific work goes here
- }
- }
- // Class4
- public class Class4 extends ClassFactoryBase implements IClassFactory
- {
- public void processWork()
- {
- // ... Class-specific work goes here
- }
- }
- }
- public with sharing class ClassFactoryManager
- {
- public ClassFactoryManager(){}
- // Return the appropriate class instance based on className
- public ClassFactory.IClassFactory newClassInstance(String className)
- {
- Type t = Type.forName(className);
- return (ClassFactory.IClassFactory) t.newInstance();
- }
- }
- > ToolingAPI x = new ToolingAPI(); ToolingAPI.ExecuteAnonymousResult
- > toolingResult = x.executeAnonymousUnencoded("Your apex code as a
- > string here");
- public class Extension implements Callable {
- // Actual method
- String concatStrings(String stringValue) {
- return stringValue + stringValue;
- }
- // Actual method
- Decimal multiplyNumbers(Decimal decimalValue) {
- return decimalValue * decimalValue;
- }
- // Dispatch actual methods
- public Object call(String action, Map<String, Object> args) {
- switch on action {
- when 'concatStrings' {
- return this.concatStrings((String)args.get('stringValue'));
- }
- when 'multiplyNumbers' {
- return this.multiplyNumbers((Decimal)args.get('decimalValue'));
- }
- when else {
- throw new ExtensionMalformedCallException('Method not implemented');
- }
- }
- }
- public class ExtensionMalformedCallException extends Exception {}
- }
- @IsTest
- private with sharing class ExtensionCaller {
- @IsTest
- private static void givenConfiguredExtensionWhenCalledThenValidResult() {
- // Given
- String className = 'Extension'; // Variable to demonstrate setting class name
- String methodName = 'multiplyNumbers'; // Variable to demonstrate setting method name
- Decimal decimalTestValue = 10;
- // When
- Callable extension = (Callable) Type.forName(className).newInstance();
- Decimal result = (Decimal) extension.call(methodName, new Map<String, Object> { 'decimalValue' => decimalTestValue });
- // Then
- System.assertEquals(100, result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement