how to create intelligent invoker :)

D

devphylosoff

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 ?
 
D

Daniel Pitts

devphylosoff said:
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 ?
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/wordpres...angers-of-reflection-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);
 
R

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top