Created
June 28, 2011 03:48
-
-
Save haikusw/1050447 to your computer and use it in GitHub Desktop.
Objective C singleton pattern templates discussion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Accessorizer's default generated singleton code for class Foo | |
static Foo *sharedInstance = nil; | |
+ (void) initialize | |
{ | |
if (sharedInstance == nil) | |
sharedInstance = [[self alloc] init]; | |
} | |
+ (id) sharedFoo | |
{ | |
//Already set by +initialize. | |
return sharedInstance; | |
} | |
+ (id) allocWithZone: (NSZone *) zone | |
{ | |
//Usually already set by +initialize. | |
@synchronized(self) | |
{ | |
if (sharedInstance) | |
{ | |
//The caller expects to receive a new object, so implicitly retain it | |
//to balance out the eventual release message. | |
return [sharedInstance retain]; | |
} else | |
{ | |
//When not already set, +initialize is our caller. | |
//It's creating the shared instance, let this go through. | |
return [super allocWithZone: zone]; | |
} | |
} | |
} | |
- (id) init | |
{ | |
//If sharedInstance is nil, +initialize is our caller, so initialize the instance. | |
//If it is not nil, simply return the instance without re-initializing it. | |
if (sharedInstance == nil) | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
//Initialize the instance here. | |
} | |
} | |
return self; | |
} | |
- (id) copyWithZone: (NSZone *) zone | |
{ | |
return self; | |
} | |
- (id) retain | |
{ | |
return self; | |
} | |
- (unsigned) retainCount | |
{ | |
return UINT_MAX; // denotes an object that cannot be released | |
} | |
- (void) release | |
{ | |
// do nothing | |
} | |
- (id) autorelease | |
{ | |
return self; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Apple's documented version (from Cocoa Fundamentals Guide) | |
// http://developer.apple.com/library/ios/#documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html%23//apple_ref/doc/uid/TP40002974-CH4-SW32 | |
static MyGizmoClass *sharedGizmoManager = nil; | |
+ (MyGizmoClass*)sharedManager | |
{ | |
if (sharedGizmoManager == nil) { | |
sharedGizmoManager = [[super allocWithZone:NULL] init]; | |
} | |
return sharedGizmoManager; | |
} | |
+ (id)allocWithZone:(NSZone *)zone | |
{ | |
return [[self sharedManager] retain]; | |
} | |
- (id)copyWithZone:(NSZone *)zone | |
{ | |
return self; | |
} | |
- (id)retain | |
{ | |
return self; | |
} | |
- (NSUInteger)retainCount | |
{ | |
return NSUIntegerMax; //denotes an object that cannot be released | |
} | |
- (void)release | |
{ | |
//do nothing | |
} | |
- (id)autorelease | |
{ | |
return self; | |
} |
thanks to all for this discussion - I have a few switches (options) in the Accessorizer Singleton code gen interface. I can re-implement/add some custom options there. People just need to email me what they want and I'll do my best to accommodate. Lastly, if there's ever a question with the code generated by Accessorizer, do NOT hesitate to contact me via email so we can discuss, modify, improve etc.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
no more overrides.
they're dangerous and scramble the normal ref counting mechanism (which isn't even available to you in ARC anyway).
If the user wants to retain/release the singleton - let him. It won't cause any harm at all.
If the user decides to release it - well that's a stupid programmer who isn't following the Obj-C reference counting rules. Not much you can or should do to prevent that.
ARC makes all this noise go away.