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

Reply via email to