In my class Deck i have
static Deck *gInstance = NULL;
+(Deck *) instance {
@synchronized(self) {
if (gInstance == NULL)
gInstance = [[self alloc] init];
}
return (gInstance);
}
and an init method that looks like
-(id) init {
if (gInstance != NULL) {
return self;
}
self = [super init];
if (self) {
// Lots of clever things
}
gInstance = self;
return self;
}
My concern here is mainly whether init is implemented correctly. Please let me know if what i wrote looks right to you.
Or … is there a way i can make init private and prevent people (myself included) from seeing it altogether?
That is a kind of odd singleton implementation. My favorite implementation is to use some newer functionality present in GCD.
I would recommend only doing that, nothing more, nothing less. Enforcing a strict singleton will only end in pain and is generally pointless and an anti-pattern.
As far as if there is anything strictly ‘wrong’ with your implementation, I believe you’d want to return
gInstancein the initializer when it isn’t NULL, not self.