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.
In Java, Singleton is a design pattern that ensures that a class can only have one object. To create a singleton class, a class must implement the following properties: Create a private constructor of the class to restrict object creation outside of the class.
Example. The Singleton pattern ensures that a class has only one instance and provides a global point of access to that instance. It is named after the singleton set, which is defined to be a set containing one element. The office of the President of the United States is a Singleton.
It is used where only a single instance of a class is required to control the action throughout the execution. A singleton class shouldn't have multiple instances in any case and at any cost. Singleton classes are used for logging, driver objects, caching and thread pool, database connections.
By using singletons in your project, you start to create technical debt. Singletons tend to spread like a virus because it's so easy to access them. It's difficult to keep track of where they're used and getting rid of a singleton can be a refactoring nightmare in large or complex projects.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With