Re: Proper way to create a singleton without @synchronized ?
Re: Proper way to create a singleton without @synchronized ?
- Subject: Re: Proper way to create a singleton without @synchronized ?
- From: Eeyore <email@hidden>
- Date: Sun, 17 Apr 2011 13:16:14 -0700
On Apr 17, 2011, at 12:58 PM, Quincey Morris wrote:
> On Apr 17, 2011, at 12:04, WT wrote:
>
>> On Apr 17, 2011, at 3:52 PM, Joanna Carter wrote:
>>
>>>
>
>>> Tell me; maybe it's my background in other languages, but I would tend to use a "static class" as a singleton; or, at least, design a class with only class methods/properties, with static "fields" declared in the @implementation section of a class.
>>>
>>> Why this fascination with going to all the trouble of creating a singleton rather than using the "static class" approach?
>
>> not a fascination, but simply a preference and being used to coding that way. As for the "static class" idea, what happens if you need/want to subclass that class? Then you have to search for and change all the places in your code base that refer to it.
>
> 1. I've found that using the class as a singleton doesn't always serve the purpose -- though I can never remember the usage case that's a problem.
The issue that recently caused me to convert a static class to a singleton was when testing my own code that relied on the singleton. I found that it was much easier to mock an object with instance variables than a class with static variables. Whether this design change was the tail wagging the dog is probably a reasonable issue to raise. Once I moved from the static class to a singleton object, I ended up taking the next step and shifting to a default object rather than a true singleton (which I think is what Quincey is addressing in his second point, quoted below). That cleaned up some code smells elsewhere, so, at least in this case, the singleton was probably the wrong pattern from the start.
> 2. FWIW, I'm generally with Kyle and others who are suggesting that trying to implement forced, generalized singleton-ness may be a practical un-necessity.
>
> A while back, I asked on this list whether a singleton is an object that there *is* only one of, or it's an object that there *can be* only one of. The answer, from someone whose opinion I respect (and who is almost always right) was "is", not "can be".
Aaron _______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden