Lazy Loading Singletons
Dejan Bosanac over at O’Reilly points out a style of initializing singletons which I hadn’t seen before.
I think there’s 3 things worth saying about this:
- Don’t use the Singleton pattern if you can avoid it. I should really dig up some references for this comment but I can’t be arsed.
- Use a static initializer if you can. It avoids having another class involved and I think it reads a bit easier.
- If you really need to have the Singleton inited on the getInstance call, then go ahead and use the on demand holder. In my experience the first call to a Singleton is the getInstance method so the static initialiser will be called at that stage anyway.
Trackbacks
Use the following link to trackback from your own site:
http://blogs.divisibleprime.com/ronin/articles/trackback/796
