I have an abstract base class and several child classes:
public abstract class Widget{ // ... public abstract void doSomething();}public class Fidget extends Widget{ // ... public void doSomething() { // ... }}public class Foo extends Widget{ // ... public void doSomething() { // ... }}public class Fizz extends Widget{ // ... public void doSomething() { // ... }}
As you can see, Fidget, Foo and Fizz all extend Widget and override its abstract doSomething() class.
In my driver, I need to load and invoke the doSomething() method of a class that is dependent on user input. My solution (so far) has been to create a WidgetFactory class that takes the user input and returns an instance of the appropriate class:
int x = getUserInput();Widget widget = WidgetFactory.getWidget(x);widget.doSomething();
My question is: is this the "correct" way of doing things? If not, what is and why?