Re: singleton pattern in cocoa
Re: singleton pattern in cocoa
- Subject: Re: singleton pattern in cocoa
- From: Bryan Henry <email@hidden>
- Date: Mon, 14 Sep 2009 12:39:54 -0400
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 (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
_______________________________________________
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