Design pattern check...

Started by
3 comments, last by OrangyTang 13 years, 8 months ago
[Java]

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?
Advertisement
It is a correct way of doing things, and typically it does pretty well. Without knowing the specifics of the classes involved, then its hard to give more than a non-committal general answer.
Depending on your structure and exactly what the Widget and 'user info' entered is, you may want to create the Widget instance via reflection (which is more complicated, but means you don't have to do as much work to maintain it when you add new subclasses).

But as rip-off says, without some concrete info we can only talk in vague generalities.
Thank you rip-off & OrangyTang.

OrangyTang, how would the code look via reflection? Let's say our Widget had a protected int member named xType:

public abstract class Widget
{
protected int xType;
// ...
}

Now let's say, that the doSomething() method in each subclass simply sets xType to a different value, particular to that class.

How would we define a reflection-based model in the Widget superclass?
Quote:Original post by plywood
Thank you rip-off & OrangyTang.

OrangyTang, how would the code look via reflection? Let's say our Widget had a protected int member named xType:

public abstract class Widget
{
protected int xType;
// ...
}

Now let's say, that the doSomething() method in each subclass simply sets xType to a different value, particular to that class.

How would we define a reflection-based model in the Widget superclass?

You're still being unhelpfully vague, but I'll assume you've got something like:

Widget getWidget(String input){  if (input.equals("fidget"))    return new Fidget();  else if (input.equals("bar"))    return new Bar();  else    return new Foo();}

In which case you might replace it with a reflection based approach:
Widget getWidget(String input){  return (Widget)Class.forName(input).newInstance();}

...and your input would be a fully-qualified class name (like "yourpackage.Fidget").

Proper error handling not shown. And you might do some more interesting things with the class lookup (like having a hardcoded package name so you only have to specify the class name).

This topic is closed to new replies.

Advertisement