• 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

New java singleton pattern implementation

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi guys,
Here is one of the singleton pattern implementation I am using regularly.
It seems to avoid most performance problems associated with the regular singleton pattern and is using lazy initialization.
As I have not seing this implementation for a while across the forum or on other web sites, I thought some of you might be interrested.
And now for the code:

Please give me some feedback on this one.
Many thanks,
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think that is a good solution, if you need lazy initialization.
But I am curious: how often do you need lazy initialization?
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And when it comes to it, how often do you really need Singleton. In my experience it is the most mistakenly-used of all the Design patterns.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Frank,
Common examples of Singleton I use are for:
- Caching of configuration files
- Caching of reference tables
- Pools of objects, e.g. Connection Pool
- Logging class
- Unique ID generation (e.g. based on an incremental counter)
- Services (e.g. socket based)
- Utility classes in general
Indeed, you could implement most of these using only static methods. However, you then lose some of the benefits of OO programing, e.g. polymorphism.
I recon, as Ilja suggests, that you should not use the above code when you don't need lazy initialization.
The ususal singleton pattern will do:

In some cases, however, lazy initialization can help performance by initializing objects only when needed.
Regards,
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Common examples of Singleton I use are for:
- Caching of configuration files
- Caching of reference tables
- Pools of objects, e.g. Connection Pool
- Logging class
- Unique ID generation (e.g. based on an incremental counter)
- Services (e.g. socket based)
- Utility classes in general

I'm not sure I'd use a Singleton for any of these.
The big problem with Singleton (just as with any other type of "global") is that it's very hard to unit test. If I were to write code which uses (for example) a Singleton for caching of reference tables, my tests would have to use the "real" Singleton implementation, which in turn would probably have to use the "real" Config cache for table names, connections and stuff, and access the "real" database. This might end up using most of the application and several seconds just to test (for example) whether my two line routine returns null or "" when a record is not present. And if someone else happens to add the missing record to the database, my test will start to fail without me changing my working code.
If you avoid Singleton, you can easily make use of "Mock" or "Dummy" implementations of any object for testing, regardless of the state of the rest of the system.
[ November 18, 2002: Message edited by: Frank Carver ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree with Frank. I just read the GoF book when (re-)starting a hobby project, and just as probably anybody else, we made heavy use of the Singleton pattern. :roll:
Though we found a way to mock the Singletons (mainly by adding a setInstance method), it still was a real PITA - regularly tests would fail because a small change in the implementation suddenly required more tests to set up/reset the Singleton in a specific way.
Part of the problem may be a general issue with coupling in the early system, but I am still thinking that you should be *very* carefull applying this pattern...
Finally, as usual a Wiki link: http://c2.com/cgi/wiki?SingletonPattern
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Franck,
Ilja,
I see the problem ...
But wouldn't it be possible to then use an abstract factory for retrieving the singleton instances, allowing you to alter its implementation during unit testing?
The drawback being the extra indirection...
Cheers,
[ November 18, 2002: Message edited by: Beno�t d'Oncieu ]
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But wouldn't it be possible to then use an abstract factory for retrieving the singleton instances, allowing you to alter its implementation during unit testing?
Sure, but if you are going to get your objects from a Factory, there's no need for them to be Singletons! It's entirely up to the Factory whether it issues the same object or different objects to its customers.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
True!
 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Frank/Ilja,

Common examples of Singleton I use are for:
- Caching of configuration files
- Caching of reference tables
- Pools of objects, e.g. Connection Pool
- Logging class
- Unique ID generation (e.g. based on an incremental counter)
- Services (e.g. socket based)
- Utility classes in general
I'm not sure I'd use a Singleton for any of these.


In a general case, what pattern WOULD you use for these types of resources. I realize it would probably be application-specific, but I am new to using design patterns and I am still trying to figure out some generalities, i.e. rules of thumb about when to use which. I definitely see the utility of patterns, but I have to admit sometimes I feel a bit thick-headed on the whole subject!
Thanks,
Eric
 
Frank Carver
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For most of those I'd just use a regular class, and pass objects in to other objects or methods as needed. If my application only needs one of something, I'd create it in main() (or init() or whatever, depending on the type of application you are writing), and pass it in to whatever objects need it.
If I find I need a mock version of one of these for testing, I'll either extend the real class and dummy up the methods, or make the client code use an interface and code both the real and mock classes to implement that interface. Then I can pass my mock objects in to the objects being tested, and test them with no dependencies on other parts of the system.
 
Eric Fletcher
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Frank, I appreciate the feedback.
Cheers,
Eric
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic