[英]How can I call an instance of a singleton class based on a properties file in java?
I have a quick Java question. 我有一个快速的Java问题。 Oh and I'm fairly new to both Java and stackoverflow, so please be considerate :)
哦,我对Java和stackoverflow还是很陌生,所以请体谅:)
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:
因此,我有两个遵循单例模式的类,即A类和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: 现在,我要从另一个类访问这些类的实例,比方说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: 拥有一个.properties文件,如下所示:
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. 然后以某种方式从该属性文件中读取类名称,以便当我将A更改为B时,我的testClassC将获得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?
哦,再次,testClassC是一个测试类,我有很多这样的JUnit测试类,那么解决这个问题的最佳方法是什么?
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. 您有一个定义类的
interface
-需要此interface
,以便可以抽象实际的实现。
You then have a factory interface which defines the newInstance
method. 然后,您将拥有一个定义
newInstance
方法的工厂接口。 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. 此方法从类路径加载属性文件-显然可以将其更改为文件系统位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.