newbie java design question

Discussion in 'Java' started by bob_roberts, Aug 30, 2006.

  1. bob_roberts

    bob_roberts Guest

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

    Thanks in advance...

    bobr
     
    bob_roberts, Aug 30, 2006
    #1
    1. Advertising

  2. bob_roberts

    Matt Rose Guest

    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
     
    Matt Rose, Aug 30, 2006
    #2
    1. Advertising

  3. bob_roberts

    Oliver Wong Guest

    "bob_roberts" <> wrote in message
    news:qMgJg.16926$...
    >- 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?


    For a "pattern", look at Factory or Abstract Factory.

    For Java support, look at
    http://java.sun.com/j2se/1.5.0/docs/api/javax/imageio/spi/ServiceRegistry.html

    So your client code would see A and the factory. The factory would use the
    ServiceRegistry to see what classes are available that implements A, and
    return an appropriate instance of that class to the client.

    - Oliver
     
    Oliver Wong, Aug 30, 2006
    #3
  4. bob_roberts

    bob_roberts Guest

    "Matt Rose" <> wrote in message
    news:...
    >
    > 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



    Thanks for the reply. That sounds like a good idea. I wasn't aware that
    the constructors could be invoked like this, hence, my newbie state.

    Oliver Wong also replied with an idea about using ServiceRegistry which I
    will
    look into as well.

    Thanks to both of you for taking the time to answer this!

    bobr
     
    bob_roberts, Aug 30, 2006
    #4
  5. "Matt Rose" <> 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
     
    Thomas Fritsch, Aug 30, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dave
    Replies:
    5
    Views:
    599
    William Brogden
    Jul 17, 2004
  2. peelman
    Replies:
    2
    Views:
    439
    anonymous
    Jan 13, 2005
  3. Replies:
    0
    Views:
    317
  4. Bartholomew Simpson

    class design/ design pattern question

    Bartholomew Simpson, Jun 12, 2007, in forum: C++
    Replies:
    2
    Views:
    453
    Daniel T.
    Jun 12, 2007
  5. Jerry C.
    Replies:
    8
    Views:
    253
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page