condition in my code and want to create singleton object.singletonExample == null
jatan bhavsar wrote:How can i optimize the creation of singleton object?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
jatan bhavsar wrote:Specially when i dont want to check if the singletonExample is null or not
Matthew Brown wrote:
But I'd still prefer creating it up front in the vast majority of cases.
Jeff Verdegan wrote:
Matthew Brown wrote:
But I'd still prefer creating it up front in the vast majority of cases.
There is in fact never a reason to lazily instantiate a singleton. The correct way to handle it is to just instantiate it at declaration. (Or, if you prefer the more modern approach, the 1-element enum shown earlier, though I personally still feel itchy when I see that.)
Mike Simmons wrote:
Jeff Verdegan wrote:
Matthew Brown wrote:
But I'd still prefer creating it up front in the vast majority of cases.
There is in fact never a reason to lazily instantiate a singleton. The correct way to handle it is to just instantiate it at declaration. (Or, if you prefer the more modern approach, the 1-element enum shown earlier, though I personally still feel itchy when I see that.)
Sounds dangerously absolutist; I can't agree with that at all. Especially when stated as a "fact". Matthew has a perfectly sensible attitude about it.
Mike Simmons wrote:I searched a bit and found places where you made the assertion. Didn't see much to back it up - but I don't claim to have done a thorough search. If there's one in particular you'd like to point out, I'm happy to take a look.
On the other hand, you do seem to grudgingly accept the 1-element enum approach, which is a lazily-instantiated singleton.
And you acknowledge that there is at least one exception somewhere to your rule.
For myself, I would consider lazy singleton initialization when
(a) I need a singleton (this is the rarest part of the requirement)
(b) It may take some time or other resources to initialize, and
(c) some/many users will never even need it
As an example, let's say I'm making a MathUtils library and want to include some methods to provide lists of prime numbers for things like factoring. I expect to keep an array of all primes from 2 to N cached in memory, and beyond N maybe read from a file, or calculate on the fly, or some combination of those. Now there's really no reason to have more than one of these in memory anywhere - the list of prime numbers is the same for everyone. But many users of my MathUtils library will never call the methods that rely on primes (we're assuming I have other nifty features to offer). Why should they wait around for that array of primes to get loaded?
I'm sure there are a variety of ways to implement this that might not fit a suitably narrow definition of singleton. Using statics everywhere comes to mind - but that tends to make it difficult to mock out this component when testing other stuff. And anyway, as a grown-up programmer who has read both editions of Effective Java, I have no problem making a working thread-safe lazy-initialized singleton using several possible techniques. What's wrong with using the tools at my disposal, if I know what I'm doing?
Jeff Verdegan wrote:I'm not saying lazy instantiation of a singleton is going to bring about the end of civilization as we know it. I'm merely asserting that there's no meaningful benefit to it.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:[suppose I have one that needs to be initialized with the latest available data - I don't know, maybe a stock market quotation or something. Once set-up, we're happy to work with what we've got; but we would like the latest available data at the time the object is first used.
Wouldn't that constitute a candidate for lazy instantiation?
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|