• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Is Apple's singleton sample code correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is Apple's singleton sample code correct?


  • Subject: Re: Is Apple's singleton sample code correct?
  • From: j o a r <email@hidden>
  • Date: Sat, 26 Nov 2005 18:39:26 +0100

Hello,

I think that this thread, while interesting from a theoretical point of view, it's just way out of line from a practical point of view. I think there are signs of over-engineering. Just look at the documentation under discussion:

<http://developer.apple.com/documentation/Cocoa/Conceptual/ CocoaObjects/Articles/CreateSingleton.html>

* Why mix in "@synchronized() {}" in this example at all? Leave that to the multi-threading topics!

* Why spend all that time on overriding retain / release, et.c.? If you're not a framework developer, I would say that it's mostly a complete waste of time.

Seriously - how often have you had a problem with your singleton pattern classes that would have been resolved by this type of safeguards? I don't think I've ever had a problem of that nature, so I don't see the benefit of adding this complexity.
I also think that it's obvious from the discussions in this tread that attempting to add these safeguards most often creates more bugs that it would possibly solve.


Just keep it simple!


@interface MySingletonClass : NSObject

+ (MySingletonClass *) sharedInstance;

@end

@implementation MySingletonClass

+ (MySingletonClass *) sharedInstance
{
	static MySingletonClass *mySharedInstance = nil;
	if (nil == mySharedInstance)
	{
		mySharedInstance = [[MySingletonClass alloc] init];
	}
	return mySharedInstance;
}

@end


j o a r


Attachment: smime.p7s
Description: S/MIME cryptographic signature

 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Shawn Erickson <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Jim Correia <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Uli Kusterer <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: Jim Correia <email@hidden>)

  • Prev by Date: Re: Is Apple's singleton sample code correct?
  • Next by Date: Re: Is Apple's singleton sample code correct?
  • Previous by thread: Re: Is Apple's singleton sample code correct?
  • Next by thread: Re: Is Apple's singleton sample code correct?
  • Index(es):
    • Date
    • Thread