问题描述
这里我有一个Singleton(使用Bill Pugh的方法,在Wiki中记录为Singleton) 。
private static class SingletonObjectFactoryHolder {
// 1
private static final ObjectFactory INSTANCE = new ObjectFactory );
}
private ObjectFactory()throws异常{
// 2
//创建工厂
}
public static ObjectFactory getInstance(){
// 3
return SingletonObjectFactoryHolder.INSTANCE;
}
如果在2处抛出异常,我想将其传播到呼叫者。
但是,我不能从第1行引发异常。
所以,如果单例对象不是,我唯一的选择是返回null对象创建正确?
谢谢
PS我意识到,如果通过不同的类加载器加载Singleton,如果加载反射,但它对我的目的是足够好的。
//更新
我很好奇,我不能重新排列我的设计,以抛出异常?
此外,我不需要任何同步(类加载器保证静态内部只有当getInstance()被调用时,类才会加载一次)。因此,线程安全和懒惰地实例化?
private static class SingletonObjectFactoryHolder {
// 1
public static ObjectFactory getInstance()throws Exception {
return new ObjectFactory();
}
}
private ObjectFactory()throws异常{
// 2
//创建工厂
}
public static ObjectFactory getInstance(){
// 3
return SingletonObjectFactoryHolder.getInstance();
}
再次感谢
使用一个静态的初始化程序,并将 Exception
作为。单击链接以阅读Javadoc,您将看到它适合于此特定的功能需求:在静态初始化期间处理异常。实际上,一个单例实际上不过是一个静态和延迟初始化的全局对象。
private static class SingletonObjectFactoryHolder {
private static final ObjectFactory INSTANCE;
static {
try {
INSTANCE = new ObjectFactory();
} catch(Exception e){
throw new ExceptionInInitializerError(e);
}
}
}
不需要成语,被认为是反模式,在某些情况下甚至不安全。
Whats the best way to design a singleton class that could throw an exception?
Here I have a Singleton (using Bill Pugh's method, documented in Wiki for Singleton).
private static class SingletonObjectFactoryHolder{
//1
private static final ObjectFactory INSTANCE = new ObjectFactory();
}
private ObjectFactory() throws Exception{
//2
//create the factory
}
public static ObjectFactory getInstance(){
//3
return SingletonObjectFactoryHolder.INSTANCE;
}
If an exception is thrown at 2, I would like to propagate it to the caller. However, I can't throw an exception from line 1.
So, is my only option to return a null object if the singleton object wasn't created correctly?
Thanks
P.S I do realize that this Singleton can break if its loaded via different classloaders or if loaded reflexively, but it's good enough for my purpose.
//UPDATE
I am curious, can I not rearrange my design as below to throw exceptions?
Also, I don't need any synchronization (the classloader guarantees that the static inner class will only loaded once and only when getInstance() is called). Thus, thread-safe and lazily-instantiated?
private static class SingletonObjectFactoryHolder{
//1
public static ObjectFactory getInstance() throws Exception{
return new ObjectFactory();
}
}
private ObjectFactory() throws Exception{
//2
//create the factory
}
public static ObjectFactory getInstance(){
//3
return SingletonObjectFactoryHolder.getInstance();
}
Thanks again.
Use a static initializer and rethrow the Exception
as ExceptionInInitializerError
. Click the link to read the Javadoc, you'll see that it suits exactly for this particular functional requirement: handling exceptions during static initialization. A singleton is in fact nothing less or more than a statically and lazily initialized global object.
private static class SingletonObjectFactoryHolder{
private static final ObjectFactory INSTANCE;
static {
try {
INSTANCE = new ObjectFactory();
} catch (Exception e) {
throw new ExceptionInInitializerError(e);
}
}
}
No need for double checked locking idiom which is considered an anti-pattern and in some circumstances even unsafe.
这篇关于单身和异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!