Created
December 16, 2008 19:14
-
-
Save pope/36728 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| import java.util.List; | |
| import java.util.ArrayList; | |
| public class Bah { | |
| public static void main(String[] args) { | |
| final Foo foo = new Foo(); | |
| final List<FunctionPointer<Object>> fps = new ArrayList<FunctionPointer<Object>>(); | |
| fps.add(new FunctionPointer<String>() { | |
| @Override | |
| public String call() { | |
| return foo.getName(); | |
| } | |
| }); | |
| fps.add(new FunctionPointer<Integer>() { | |
| @Override | |
| public Integer call() { | |
| return foo.getAge(); | |
| } | |
| }); | |
| for (FunctionPointer<Object> fp : fps) { | |
| System.out.println(fp.call()); | |
| } | |
| // The Enum Way | |
| List<FooEnum> enums = new ArrayList<FooEnum>(); | |
| enums.add(FooEnum.NAME); | |
| enums.add(FooEnum.AGE); | |
| for (FooEnum e : enums) { | |
| System.out.println(e.call()); | |
| } | |
| } | |
| } |
This file contains hidden or 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 class Foo { | |
| public String getName() { return "Name"; } | |
| public Integer getAge() { return 28; } | |
| } |
This file contains hidden or 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 enum FooEnum { | |
| NAME() { | |
| @Override | |
| public Object call(Foo foo) { | |
| return foo.getName(); | |
| } | |
| } | |
| AGE() { | |
| @Override | |
| public Object call(Foo foo) { | |
| return foo.getAge(); | |
| } | |
| } | |
| public abstract Object call(Foo foo); | |
| } |
This file contains hidden or 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 FunctionPointer<T> { | |
| T call(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment