How to decide at runtime which concrete class implementation to instantiate

Discussion in 'Java' started by java-john, Mar 7, 2006.

  1. java-john

    java-john Guest

    I have a class that uses composition of interfaces. I want to be able
    to allow users to decide at run-time which implementations of these
    interfaces to instantiate. What is the best way to do this? Are there
    any frameworks out there? Is there some design pattern for this?

    For example:

    public interface MyInterface {
    public String getValue();
    }

    public class MyClass {
    private MyInterface myInterface;
    public MyClass() {
    myInterface = new SomeRunTimeImplementationOfMyInterface();
    }
    }
     
    java-john, Mar 7, 2006
    #1
    1. Advertising

  2. java-john

    Ed Kirwan Guest

    Re: How to decide at runtime which concrete class implementationto instantiate

    java-john wrote:
    > I have a class that uses composition of interfaces. I want to be able
    > to allow users to decide at run-time which implementations of these
    > interfaces to instantiate. What is the best way to do this? Are there
    > any frameworks out there? Is there some design pattern for this?
    >
    > For example:
    >
    > public interface MyInterface {
    > public String getValue();
    > }
    >
    > public class MyClass {
    > private MyInterface myInterface;
    > public MyClass() {
    > myInterface = new SomeRunTimeImplementationOfMyInterface();
    > }
    > }
    >


    This usually depends on your product's configuration, which in turn
    usually depends on the user's choice of product options either when the
    software is run, or on the options-window during execution.

    This will usually involve the software's reading a file of options when
    it's started, and there are innumerable ways of doing this: Properties,
    Preferences, a roll-your-XML file, etc. (Even good-ol' command-line
    parameters, though not recommended.)

    The point is that, whatever means is used to input these options to the
    software, somewhere in the code there will be decision points based on them.

    A very basic example, if you have your configuration file as a text file
    specifying whether to display graphics on the user screen or not, the
    configuration file might be as simple as:
    graphics=off

    And the code will be something like:

    Class Start {
    void readConfiguraionFile() {
    Properties properties = new Properties();
    try {
    properties.load(new
    FileInputStream("filename.properties"));
    } catch (IOException e) {
    }
    String graphicsOption = properties.getProperty("graphics");
    GraphicsFacade graphics = null;
    if (graphicsOptions.equals("on")) {
    graphics = new FullGraphicsDisplay();
    } else {
    graphics = new TextOnlyDisplay();
    }
    store(graphics);
    }
    }

    Class SomethingElse {
    void welcomeScreen() {
    GraphicsFacade graphics = retrieveGraphics();
    graphics.showUserWelcome();
    }
    }

    --
    www.EdmundKirwan.com - Home of The Fractal Class Composition.

    Download Fractality, free Java code analyzer:
    www.EdmundKirwan.com/servlet/fractal/frac-page130.html
     
    Ed Kirwan, Mar 7, 2006
    #2
    1. Advertising

  3. java-john

    Guest

    java-john wrote:

    > I have a class that uses composition of interfaces. I want to be able
    > to allow users to decide at run-time which implementations of these
    > interfaces to instantiate. What is the best way to do this? Are there
    > any frameworks out there? Is there some design pattern for this?
    >
    > For example:
    >
    > public interface MyInterface {
    > public String getValue();
    > }
    >
    > public class MyClass {
    > private MyInterface myInterface;
    > public MyClass() {
    > myInterface = new SomeRunTimeImplementationOfMyInterface();
    > }
    > }


    Hi,

    You can create something like Abstract Factory pattern in which u can
    create factory for your Interfaces depending upon some user parameter.

    --Vikas
     
    , Mar 7, 2006
    #3
  4. java-john

    Roedy Green Guest

    On 7 Mar 2006 07:10:19 -0800, "java-john" <>
    wrote, quoted or indirectly quoted someone who said :

    >I have a class that uses composition of interfaces. I want to be able
    >to allow users to decide at run-time which implementations of these
    >interfaces to instantiate. What is the best way to do this? Are there
    >any frameworks out there? Is there some design pattern for this?


    You can have a look at how JCE is implement with a pluggable
    interface.
    See http://mindprod.com/jgloss/jce.html

    another one is the way you can register additional URL handlers.
    see http://mindprod.com/jgloss/protocolhandler.html

    Another is the way you can register additional encodings.
    http://mindprod.com/jgloss/encoding.html#ROLLYOUROWN
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
     
    Roedy Green, Mar 7, 2006
    #4
  5. java-john

    Oliver Wong Guest

    "java-john" <> wrote in message
    news:...
    >I have a class that uses composition of interfaces. I want to be able
    > to allow users to decide at run-time which implementations of these
    > interfaces to instantiate. What is the best way to do this? Are there
    > any frameworks out there? Is there some design pattern for this?
    >
    > For example:
    >
    > public interface MyInterface {
    > public String getValue();
    > }
    >
    > public class MyClass {
    > private MyInterface myInterface;
    > public MyClass() {
    > myInterface = new SomeRunTimeImplementationOfMyInterface();
    > }
    > }
    >


    The most straightforward approach is something like this:

    <pseudocode>
    public class FactoryClass {
    public Alpha createAlpha() {
    if (userChoseAlphaFoo) {
    return new AlphaFoo();
    } else if (userChoseAlphaBar) {
    return new AlphaBar();
    } // etc.
    }

    public Beta createBeta() {
    if (userChoseBetaFoo) {
    return new BetaFoo();
    } else if (userChoseBetaBar) {
    return new BetaBar();
    } /*etc.*/
    }
    }
    </pseudocode>

    This works well if the user can independently choose which concrete
    class for every implementation. However, if the user makes one choice, and
    it affects ALL your interfaces as a set, you'll want to use the Abstract
    Factory interface:

    <pseudocode>
    public interface Factory {
    public Alpha createAlpha();
    public Beta createBeta();
    }

    public class FooFactory implements Factory {
    public Alpha createAlpha() {
    return new AlphaFoo();
    }

    public Beta createBeta() {
    return new BetaFoo()
    }
    }

    public class BarFactory implements Factory {
    public Alpha createAlpha() {
    return new AlphaBar();
    }

    public Beta createBeta() {
    return new BetaBar()
    }
    }

    /*usage:*/

    final Factory myFactory;
    if (userChoseFoo) {
    myFactory = new FooFactory();
    } else if (userChoseBar) {
    myFactory = new BarFactory();
    }

    Alpha a = factory.createAlpha();
    Beta b = factory.createBeta();
    </pseudocode>

    With abstract factories, you can ensure your set "matches" (that is, if
    you have a FOO variant of Alpha, then the Beta will also be a FOO variant,
    and not a BAR variant).

    - Oliver
     
    Oliver Wong, Mar 7, 2006
    #5
  6. java-john

    java-john Guest

    Thanks All,

    After reading your posts and doing more research, I found that the
    problem I have has been addressed already. I keep seeing the following
    keywords: service locator, inversion of control (IOC), and dependency
    injection.

    One of the frameworks that I see popping up to handle this type of
    problem is Spring. However, I have read some posts cautioning against
    "coding to avoid dependency on Spring" and I don't think something like
    Spring may be appropriate for my relatively small-scale project.
     
    java-john, Mar 7, 2006
    #6
  7. java-john

    Guest

    > After reading your posts and doing more research, I found that the
    > problem I have has been addressed already. I keep seeing the following
    > keywords: service locator, inversion of control (IOC), and dependency
    > injection.


    Service locators and IoC containers are not the same as abstract
    factories.

    As other people already pointed out, what you probably want to
    solve your problem is the pattern called "abstract factory".

    You can also use, say, an IoC container in your project: at
    some point in your program you tell the container "I want
    an object implementing MyInterface" and it will give one to
    you... According he has one (for example you explicitely
    called a factory and registered the object in the IoC
    container) or knows how to create one (for example you
    registered some factory in a container "smart enough"
    to automatically call factory methods).

    So it's not "one or the other": using service locators/IoC containers
    dont automagically render factories irrelevant.
     
    , Mar 7, 2006
    #7
  8. java-john

    java-john Guest

    lewmania,

    I think you misunderstand my problem. I have an interface. I have
    implemented the interface. However, at runtime (or somewhere
    downstream), I want other users of the API to substitute one of their
    implementation for mine. This is more in line with dependency injection
    than abstract factories pattern.

    Thanks.
     
    java-john, Mar 8, 2006
    #8
    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. Chao-Jen Chen
    Replies:
    1
    Views:
    413
    Tor Iver Wilhelmsen
    May 15, 2005
  2. Joe Kehnast
    Replies:
    1
    Views:
    624
    Martin Honnen
    Jun 11, 2004
  3. Wat
    Replies:
    2
    Views:
    616
    Cy Edmunds
    Dec 4, 2004
  4. Karim
    Replies:
    0
    Views:
    198
    Karim
    Nov 23, 2011
  5. Amit
    Replies:
    2
    Views:
    165
Loading...

Share This Page