Skip to content

Instantly share code, notes, and snippets.

@pope
Created December 16, 2008 19:14
Show Gist options
  • Save pope/36728 to your computer and use it in GitHub Desktop.
Save pope/36728 to your computer and use it in GitHub Desktop.
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());
}
}
}
public class Foo {
public String getName() { return "Name"; }
public Integer getAge() { return 28; }
}
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);
}
public interface FunctionPointer<T> {
T call();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment