You could use a Map<String, Supplier<A>>
:
Map<String, Supplier<A>> map = new HashMap<>();
map.put("A1", () -> new A1());
map.put("A2", () -> new A2());
...
public A createA(final String id) {
Supplier<A> supplier = map.get(id);
if (supplier != null) {
return supplier.get();
}
throw new IllegalArgumentException("unknown id: " + id);
}
This uses a standard Java 8 Supplier interface, with a Java 8 lambda syntax, but you could of course define your own Supplier interface, and create instances using anonymous inner classes.