Re: newbie java design question
- From: "Matt Rose" <matt.rose.at@xxxxxxxxx>
- Date: 30 Aug 2006 08:56:02 -0700
bob_roberts wrote:
- new to java (from C++ world)
Setup:
1. Abstract class A
2. Child C inherits from A.
3. Child D inherits from A.
In a client, I want to determine which child implementation to use
(via a config file, for instance). The main catch is that I do not
want any dependencies on either C or D for the client. That is, if
C is an implementation that invokes third-party code, the client which
decided (via config) to invoke C should make no reference to jar files
for child D (or any of its third-party jar files).
I've been looking into the URLClassLoader option for this, since I could
dynamically determine which implementation class to load and have at it.
In C++, I could just use dlopen() on a shared object, and load a
library(ies)
which included references to third-party libraries. Then I would
reference the loaded child class through a base class pointer and I don't
have any dependencies on shared objects which may not exist
on the system in which I'm currently running the client. I'm kind of
looking for something along those lines.
Is there an accepted "pattern" or general rule of thumb that I should look
into?
Or have I completely missed some key point in Java which would
allow me to do this more easily?
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;
Is that what you're after?
Matt
.
- Follow-Ups:
- Re: newbie java design question
- From: Thomas Fritsch
- Re: newbie java design question
- From: bob_roberts
- Re: newbie java design question
- References:
- newbie java design question
- From: bob_roberts
- newbie java design question
- Prev by Date: Re: JScrollPane Scrollbar issues
- Next by Date: Re: Beginner's question
- Previous by thread: newbie java design question
- Next by thread: Re: newbie java design question
- Index(es):
Relevant Pages
|
|