• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Initialization of a singleton object

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

I have a question about Singleton objects and their initialization in a particular case.
I have a singleton class that has to be configured by providing a parameter through an init(...)
method that depends from a value in a configuration file at the startup of the application and that will never
change for the entire lifetime of the application. What I have to do in case of getInstance() method
invocation before that the init method has been called? Throw an exception? Which one? Or what?

Thanks!
Baltico
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think it's a bad design to be able to create an object that is not ready to be used. Can't your constructor set some value to make it usable?
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to throw an exception in that case, then look through the ones defined in the java.lang package and choose one which looks suitable. Make sure you distinguish between checked and unchecked exceptions when making your choice.
 
Michele Dibenedetto
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your answers.

The object cannot be made usable before getting the initialization parameter.
The initialization value will be a factory class instance that will be used by the singleton instance object to produce an object
used in all its public methods.

For what concerns throwing an exception I was thinking about IllegalStateException but it is unchecked and if possible I
prefer to throw a checked exception...

Maybe there is a bug in my design?

Thanks,
Baltico
 
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


 
Michele Dibenedetto
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Jimmy,

thanks for your help but my scenario is a bit more complex; here it is:

Consider 3 threads that can execute concurrently the following and that the name of the Factory class "org.baltico.FactoryImpl" is known only
at runtime (taken from configuration file or something else)

Thread1 : new Object0.doSomething("org.baltico.FactoryImpl");
Thread2 : new Object1.doSomething();
Thread3 : new Object2.doSomething();



 
Paul Clapham
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Michele Dibenedetto wrote:Maybe there is a bug in my design?



I would say that allowing other code to use your singleton object before it has been properly initialized might well qualify as a bug in your design. You should try to ensure this doesn't happen.
 
Jimmy Clark
Ranch Hand
Posts: 2187
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Michele, if the program requirements involve "concurrent" computation then the Simpleton design pattern is not appropriate. Simpleton is for simple things and a good academic exercise. In real-world enterprise computing, Simpleton implementations should be avoided.

Aside, Simpleton is easy for computer science professors to understand and easy for students to understand. However, it is a compensation for old-style procedural/sequential programming and not a true "object"-oriented design pattern. Unfortunately, computer science graduates come fresh out the gate ready to code a Singleton unaware of its limitations and associated problems, e.g. your design bug is rooted in trying to implement Singleton.

http://www.amazon.com/Java-Concurrency-Practice-Brian-Goetz/dp/0321349601

Paperback: 384 pages
ISBN-10: 0321349601
ISBN-13: 978-0321349606
reply
    Bookmark Topic Watch Topic
  • New Topic