Selecting the implementation of a java interface based on a config value

35 views Asked by At

I'm trying to selectively pick the implementation of an interface based a config value which is set in the client. Is it possible to program in java that fulfils the following:

public class A {
    private final Conn ctype;

    A() {
      ctype = B.getConn();
    }
}

public class B {
    private boolean useLatestVersion = Config.get("latest_version");//get value from config

    public static Conn getConn() {
        Conn connection = createConn(useLatestVersion);
        return connection;
    }

    private Conn createconn(Boolean value) {
        jakarta.jms.Connection jakartaConn;
        javax.jms.Connection javaxConn;
        if(value) {
            jakartaConn = //someway to get jakarta connection.
            return (Conn)jakartaConn;
        } else {
            javaxConn = //someway to get javax connection.
            return (Conn)javaxConn;
        } 
    }
}

The jakarta.jms.Connection and javax.jms.Connection are interfaces and they're implemented in our client code already. How to ensure that I get a common connection type and still redirect it to either jakarta or javax appropriately based on the what connection object is returned?

I tried with implementing an interface but the impact on the client that was using connection was huge. I wanted to minimize it as much as I can.

0

There are 0 answers