Someone asked me if I can create factory pattern in java without using If-else construct. So I come with the following. Please provide your inputs if this seems a good example for using factories.
public enum EnumButtonFactory {
RADIO(RadioButton.class),
SUBMIT(SubmitButton.class),
NORMAL(NormalButton.class);
private Class<? extends Button> button;
EnumButtonFactory(Class<? extends Button> b) {
this.button = b;
}
public Button get() {
try {
return button.newInstance();
} catch (InstantiationException | IllegalAccessException e) {
e.printStackTrace();
}
return null;
}
}