Re: Class.forName().newInstance() vs new
On 06/11/2011 08:04 PM, Patricia Shanahan wrote:
On 6/11/2011 10:49 AM, Robert Klemme wrote:
On 11.06.2011 19:13, Abu Yahya wrote:
...
else { // handle unrecognized
Class<?> k = Class.forName(dbMgrClassName);
db = (DB) k.newInstance();
}
...
Doing if else cascades is certainly the worst thing - not so much
because of the performance but because of the deficiency that you need
to touch the code every time you want to add a new class. With the other
two approaches you do not have to do that.
...
As coded, the forName code was left in as a fall-through handler, so it
new classes can be handled without touching the code. Of course, that
also means it has all the complications of both the if-else approach and
the forName approach.
Thanks for hinting at this detail, Patricia! Performance of this piece
of code can vary dramatically with configured class / handler name.
That adds a level of complication which might make tracking issues
harder ("It was so fast in our lab, now on production it's awfully slow.").
Also I find the inconsistency about what needs to be configured
irritating: the string can either be a logical name of a handler *or* a
class name - and you cannot even distinguish them easily (handler names
presented so far are also valid class names). IMHO for this it would be
better to have two different properties with clear defined precedence
for logical handler name property. That would also make it explicit
that there are two mechanisms involved. Even though, I still prefer one
of the two uniform approaches (class name, factory class name).
Kind regards
robert