简体   繁体   中英

How can I call an instance of a singleton class based on a properties file in java?

I have a quick Java question. Oh and I'm fairly new to both Java and stackoverflow, so please be considerate :)

Let me try to explain my question a little better. So I have two classes that follow a singleton pattern, say class A and class B:

public class A
{
    private static final A INSTANCE = new A()

    public static A getInstance()
    {
         return A.INSTANCE;
    }
}

public class B
{
    private static final B INSTANCE = new B()

    public static B getInstance()
    {
         return B.INSTANCE;
    }
}

Now I'm accessing instances of these classes from another class, let's say C:

public class testClassC
{
    A class_instance = A.getInstance();
    //or
    //B class_instance = B.getInstance();
}

What I'm trying to achieve is this (the syntax might be totally wrong at this point, I've been trying different things and none of them worked for me):

Have a .properties file as follows:

 className=A

And then somehow read the class name from that properties file so that when I change A to B, my testClassC will get me an instance of B.

How can I achieve this? Oh and again testClassC is a test class and I have a whole bunch of those JUnit test classes so what would be the best way to approach this?

Thank you

Here is an example using an abstract factory pattern.

You have an interface which defines your class - you need this so that you can abstract the actual implementation.

You then have a factory interface which defines the newInstance method. This makes it very easy to swap a singleton pattern for a factory pattern. It also makes it easy it easy to change the factory implementation for testing.

interface MyInterface {
}

interface MyInterfaceFactory {

    MyInterface newInstance();
}

class MyInterfaceFactoryFromProperties implements MyInterfaceFactory {

    final Class<? extends MyInterface> myInterfaceImpl;

    {
        final Properties properties = new Properties();
        try {
            properties.load(MyInterfaceFactoryFromProperties.class.getResourceAsStream("className.properties"));
        } catch (IOException ex) {
            throw new ExceptionInInitializerError(ex);
        }
        final String className = properties.getProperty("class.name");
        try {
            myInterfaceImpl = (Class<? extends MyInterface>) MyInterfaceFactoryFromProperties.class.forName(className);
        } catch (ClassNotFoundException ex) {
            throw new ExceptionInInitializerError(ex);
        }
    }

    @Override
    public MyInterface newInstance() {
        try {
            return myInterfaceImpl.newInstance();
        } catch (InstantiationException ex) {
            throw new RuntimeException(ex);
        } catch (IllegalAccessException ex) {
            throw new RuntimeException(ex);
        }
    }
}

So your factory implementation would follow this pattern but you could, for example, cache the value to make it a singleton.

This method loads the properties file from the classpath - obviously this can be changed to a filesystem location.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM