Re: newbie java design question



"Matt Rose" <matt.rose.at@xxxxxxxxx> wrote:

If you're not using something like Spring, (or even if you are!) you
could reflect the class.

Something along the lines of:

//read the fully qualified classname from your config file into
AImplClassName
Class aImpl = Class.forName(AImplClassName);
Constructor[] constructors = aImpl.getConstructors();
//Choose which constructor you want to invoke, create an Object[] with
the arguments for it called args
A a = (A) chosenConstructor.newInstance(args);
return a;

If the implementation classes have a no-arguments constructor, this can be
simplified even further:
A a = (A) Class.forName(AImplClassName).newInstance();
return a;

--
Thomas


.