Possible duplicate:
What is the closest function pointer replacement in Java?
I am writing a factory class to create a bunch of different widgets. For argument, suppose this factory can create 1000 widgets.
myWidget createWidget (widgetId)
{
case 0: createwidget0 ();
.
.
case 1000: createwidget1000 ();
}
I do not want to write an expression of 1000 cases. I want to put all the creation procedures in an array. Use widgetId as an index to directly execute the creation procedure; it does not need to pass a comparison of 1000 conditions. So, the whole createWidget procedure can be simplified, for example,
myWidget createWidget (widgetId)
{
myAwsomeFuncArr createFunc;
myWidget widget = createFunc[widgetId] ();
}
Is there any way to do this in Java?
source
share