views:

79

answers:

4

I have a bunch of classes extending an abstract Base class. Each subclass takes an array as in the constructor, (different length depending on class). These classes could be written by other people.

What is the best way to figure out the length of the array the class needs? I could: (A) Require that each derived class have a static method, returning the length. However, the base class cannot enforce this, since abstract static methods does not work in java. (B) Each derived class have a constructor with no arguments, and I construct such classes just to be able to call the countParameters() method, that I can enforce from the Base class. This feels "cludgy", since I am not interested in creating such object, but only need some info about it.

The reason is that I am creating a GUI, that gives the user the ability to create instances of Derived classes, but each Derived class takes different number of parameters. That is, I need to know how to draw the GUI before I can create the classes.

EDIT: I could just require that each Derived class have a private constructor, with no arguments, and using reflection I can call the countParameters() method.

EDIT2: Actually, what I am interested in, is what the names of the parameters are. That is, if the class Derived have the constructor

public Derived(double name1,double name2,...)

I need a way to generate the String[] array

{name1,name2,...}

I guess this would be impossible to do without creating an instance of the class, but for the user to be able to create such class, he/she needs the parameter names! Moment 22.

A: 

It's not quite clear what you're trying to achieve, but I wonder: Do the subclasses really have to take a single parameter with an array, as opposed to a list of parameters?

Constructor<?> ctor = Test.class.getConstructors()[0];
int parameterCount = ctor.getParameterTypes().length;
ctor.newInstance(new Object[parameterCount]);
meriton
That approach is probably much nicer, that way, I can examine the class, and find number of params without actually creating it.
Paxinum
Oh, but it does not really solve my problem though, see my second edit.
Paxinum
This is probably the closest I can get;There is no way for me to find any more info from a class without creating it.
Paxinum
+5  A: 

It sounds like you need the Factory Pattern.

In general, it's a bad idea for a base class to know the set of it's descendant's. So you define another class whose job it is to know that.

If you have something like a Shape, with ThisShape and ThatShape as derived classes, then a ShapeCreator will handle the job of creating the specific set of shapes your program supports, giving each one the arguments it needs.

soru
A: 
how about this code:

public absract Base {

   public abstract int size();

   public Base(Object[] objs) {
      if (objs.length != size()) {
         throw new IllegalArgumentException();
      }
    //rest of your code.
}

each child class needs to implement size method.

hope its help.

mohammad shamsi
A: 

I'd go with method A. You can't get the compiler to enforce the existence of such a method, but you can certainly enforce it in your program - no method, no work!

Seriously, this whole scheme is a bit brittle and I can't think of a way to make it significantly better. An incorrect implementation of those subclasses will bomb out, that's life.

A possible remedy would be for you to provide a set of interfaces for those subclasses, such as

SubClassTaking2Args
SubClassTaking3Args
...

and requiring your sub's to implement one of those as a marker interface. But that's just more bureaucracy with little more effect.

Carl Smotricz