Assuming there is a DrawPanel interface then it would be possible to use a factory pattern:
public class DrawPanelFactory() {
public DrawPanel create(int whichTypeOfPanel) {
if (whichTypeOfPanel == 1) {
return new DrawPanel1();
}
...
if (whichTypeOfPanel == 12) {
return new DrawPanel12();
}
throw new IllegalArgumentException("Unsupported panel type:" + whichTypeOfPanel);
}
}
That ends up being a lot of if statements, but still easily testable. To avoid the if statements use a static Map<Integer, DrawPanelFactoryDelegate> to associate a specific integer value with a specific factory that knows how to create that specific type of DrawPanel:
public class DrawPanelFactory() {
private static Map<Integer, DrawPanelFactoryDelegate> drawPanelFactories = new ...;
static {
drawPanelFactories.put(1, new DrawPanelFactory1());
...
drawPanelFactories.put(12, new DrawPanelFactory12());
}
public DrawPanel create(int whichTypeOfPanel) {
DrawPanelFactoryDelegate delegateFactory = drawPanelFactories.get(whichTypeOfPanel);
if (delegateFactory != null) {
return delegateFactory .create();
}
throw new IllegalArgumentException("Unsupported panel type:" + whichTypeOfPanel);
}
}
interface DrawPanelFactoryDelegate {
public DrawPanel create();
}
DrawPanel1Factory implements DrawPanelFactoryDelegate {
public DrawPanel create() {
return DrawPanel1();
}
}
...
DrawPanel12Factory implements DrawPanelFactoryDelegate {
public DrawPanel create() {
return DrawPanel12();
}
}
then in use:
DrawPanelFactory drawPanelFactory = new DrawPanelFactory();
DrawPanel aDrawPanel = drawPanelFactory.create(1);
...
DrawPanel yetAnotherDrawPanel = drawPanelFactory.create(12);
DrawPanel1such that it is divergent and completely different fromDrawPanel12?DrawPanelinterface, withDrawPanel1,DrawPanel2, ... implementations? If yes, see the Factory pattern.DrawPanel1.getClass().getSimpleName();