hey
problem:
how invoke diffrent methods/contructors (depends on arguments number)
in one statement.
description:
I have many classes with constructors NameOfClass(int, int, int, ...,
int);
and I have method which invoke each of above class in switch block:
if (className == sth) {
NameOfClass1(int a, int b);
} else if (className == sth2) {
NameOfClass2(int a, int b, int c);
} else if ...
use ArrayList or another like solution is not acceptable.
is reflection will be helpful here ?
Daniel Pitts - 05 Nov 2007 19:47 GMT
> hey
>
[quoted text clipped - 16 lines]
>
> is reflection will be helpful here ?
You might look into the Factory pattern instead. Why isn't the use of
ArrayList or another like solution not acceptable? Sounds like a fairly
artificial constraint. If className is a string, you could use
reflection, but I wouldn't recommend it.
<http://virtualinfinity.net/wordpress/program-design/2007/01/11/the-dangers-of-re
flection-or-put-down-that-mirror/>
Alternatively, you could use the Factory pattern and have a Map<String,
Factory>, where Factory is your base-class/interface that has a method
which will invoke new SomeClass(whatever,args,you,want);

Signature
Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
Roedy Green - 05 Nov 2007 23:54 GMT
>how invoke diffrent methods/contructors (depends on arguments number)
>in one statement.
If your parms are all ints, you could use int...
which works as if it were int[]
That lets you have a variable number of arguments to your factory.
Otherwise you will need all manner of overloaded factory methods with
the various combinations of parms wrapped around a DEBE factory.
see http://mindprod.com/jgloss/debe.html

Signature
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com