Created
October 29, 2012 21:32
-
-
Save zachelrath/3976677 to your computer and use it in GitHub Desktop.
Dynamic Method invocation in Apex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public interface Executable { | |
void execute(String method); | |
} | |
public class DoAwesomeStuff implements Executable { | |
public override void execute(String method) { | |
if (method == 'method1') method1(); | |
else if (method == 'method2') method2(); | |
} | |
private void method1(){ | |
/* method 1 body */ | |
} | |
private void method2(){ | |
/* method 2 body */ | |
} | |
} | |
public class RidiculousStuff implements Executable { | |
public override void execute(String method) { | |
if (method == 'foo') foo(); | |
else if (method == 'bar') bar(); | |
} | |
private void foo(){ | |
/* do foo */ | |
} | |
private void bar(){ | |
/* do bar */ | |
} | |
} | |
public class Main { | |
public Main() { | |
Map<String,String> params = ApexPages.currentPage().getParameters(); | |
System.Type t = System.Type.forName(params.get('class')); | |
if (t == null) return; | |
Executable x = (Executable) t.newInstance(); | |
x.execute(params.get('method')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment