singleton pattern in cocoa
Hi all, As I come from java world, I was trying to implement the singleton pattern, very usual in java. After searching in internet I found this code from wikipedia: @interface MySingleton : NSObject { } + (MySingleton *)sharedSingleton; @end @implementation MySingleton static MySingleton *sharedSingleton; + (MySingleton *)sharedSingleton { @synchronized(self) { if (!sharedSingleton) [[MySingleton alloc] init]; return sharedSingleton; } } +(id)alloc { @synchronized(self) { NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton."); sharedSingleton = [super alloc]; return sharedSingleton; } } @end What do you think about this implementation? I'm newbie with cocoa and I'm not sure. Thanks. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: singleton pattern in cocoa
This question usually gets asked at least a couple times a month. I suggest you look up the previous responses on CocoaBuilder (http://www.cocoabuilder.com/ ), which archives responses on this mailing list if you want an exhaustive response, since all solutions have been mentioned somewhere here before. - Bryan On Sep 10, 2009, at 4:07:36 PM, Manuel Grau wrote: Hi all, As I come from java world, I was trying to implement the singleton pattern, very usual in java. After searching in internet I found this code from wikipedia: @interface MySingleton : NSObject { } + (MySingleton *)sharedSingleton; @end @implementation MySingleton static MySingleton *sharedSingleton; + (MySingleton *)sharedSingleton { @synchronized(self) { if (!sharedSingleton) [[MySingleton alloc] init]; return sharedSingleton; } } +(id)alloc { @synchronized(self) { NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton."); sharedSingleton = [super alloc]; return sharedSingleton; } } @end What do you think about this implementation? I'm newbie with cocoa and I'm not sure. Thanks. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/bryanhenry%40mac.com This email sent to bryanhe...@mac.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: singleton pattern in cocoa
Singleton implementation is something that you see debated back and forth every now and then. People argue about the values over overriding -retain, -release, -autorelease, and so on. I really like Peter Hosey's analysis (and implementation) that he's posted on his blog: http://boredzo.org/blog/archives/2009-06-17/doing-it-wrong Cheers, Dave On Sep 10, 2009, at 2:07 PM, Manuel Grau wrote: Hi all, As I come from java world, I was trying to implement the singleton pattern, very usual in java... ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: singleton pattern in cocoa
About the only thing that I'd recommend is that the "static" be inside the +sharedSingleton method so that you aren't able to take the cheap- way-out and call that variable directly in other class methods before it's created. For the rest of it: it seems to follow Apple's patterns - for instance, [NSNotificationCenter defaultCenter] and [NSStatusBar systemStatusBar] and [UIAccelerometer sharedAccelerometer] and sharedAddressBook... etc A lot of things use the sharedMyClass, but a lot (though fewer) of things also use their own name. That method of singletons is pretty common though. On 10 Sep 2009, at 1:07 PM, Manuel Grau wrote: Hi all, As I come from java world, I was trying to implement the singleton pattern, very usual in java. After searching in internet I found this code from wikipedia: @interface MySingleton : NSObject { } + (MySingleton *)sharedSingleton; @end @implementation MySingleton static MySingleton *sharedSingleton; + (MySingleton *)sharedSingleton { @synchronized(self) { if (!sharedSingleton) [[MySingleton alloc] init]; return sharedSingleton; } } +(id)alloc { @synchronized(self) { NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton."); sharedSingleton = [super alloc]; return sharedSingleton; } } @end What do you think about this implementation? I'm newbie with cocoa and I'm not sure. Thanks. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/kd7qis%40gmail.com This email sent to kd7...@gmail.com ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: singleton pattern in cocoa
Definition of true singleton is generally discouraged and not really useful. See more info about it here: http://eschatologist.net/blog/?p=178 Le 10 sept. 2009 à 22:07, Manuel Grau a écrit : Hi all, As I come from java world, I was trying to implement the singleton pattern, very usual in java. After searching in internet I found this code from wikipedia: @interface MySingleton : NSObject { } + (MySingleton *)sharedSingleton; @end @implementation MySingleton static MySingleton *sharedSingleton; + (MySingleton *)sharedSingleton { @synchronized(self) { if (!sharedSingleton) [[MySingleton alloc] init]; return sharedSingleton; } } +(id)alloc { @synchronized(self) { NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton."); sharedSingleton = [super alloc]; return sharedSingleton; } } @end What do you think about this implementation? I'm newbie with cocoa and I'm not sure. Thanks. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/devlists%40shadowlab.org This email sent to devli...@shadowlab.org -- Jean-Daniel ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com
Re: singleton pattern in cocoa
Probably best to use Apple's example: http://developer.apple.com/mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW32 I love Google, but I always prefer to look at Apple's docs first. I found the above by going to developer.apple.com and typing in "Singleton" in the search field. On Sep 10, 2009, at 3:07 PM, Manuel Grau wrote: Hi all, As I come from java world, I was trying to implement the singleton pattern, very usual in java. After searching in internet I found this code from wikipedia: @interface MySingleton : NSObject { } + (MySingleton *)sharedSingleton; @end @implementation MySingleton static MySingleton *sharedSingleton; + (MySingleton *)sharedSingleton { @synchronized(self) { if (!sharedSingleton) [[MySingleton alloc] init]; return sharedSingleton; } } +(id)alloc { @synchronized(self) { NSAssert(sharedSingleton == nil, @"Attempted to allocate a second instance of a singleton."); sharedSingleton = [super alloc]; return sharedSingleton; } } @end What do you think about this implementation? I'm newbie with cocoa and I'm not sure. Thanks. ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/alex%40webis.net This email sent to a...@webis.net Alex Kac - President and Founder Web Information Solutions, Inc. "The optimist proclaims that we live in the best of all possible worlds; and the pessimist fears this is true." -- James Clabell ___ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com