• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: is this enough for a singleton? (for a class cluster "placeholder")
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: is this enough for a singleton? (for a class cluster "placeholder")


  • Subject: Re: is this enough for a singleton? (for a class cluster "placeholder")
  • From: Shawn Erickson <email@hidden>
  • Date: Fri, 13 Feb 2004 09:56:20 -0800

Another spin on a "hardcore" singleton abstract class (I just started using this last night so I hope no bugs exists but...). I added a few extra overrides based on Shaun Wexlers code, hey that is what makes it "hardcore" after all.

@interface FTSWAbstractSingleton : NSObject {
}
+ (id)singleton; //consider private - wrap with "sharedXxxx" class method
+ (id)singletonWithZone:(NSZone*)zone; //consider private

- (id)initSingleton; //designated initializer, overrides must call super's implementation
@end

@implementation FTSWAbstractSingleton

static NSMutableDictionary *s_FTSWAbstractSingleton_singletons = nil;

+ (void)initialize
{
@synchronized(self) {
if (s_FTSWAbstractSingleton_singletons == nil) {
s_FTSWAbstractSingleton_singletons = [[NSMutableDictionary alloc] init];
}
}
}

+ (id)singleton
{
return [self singletonWithZone:[self zone]];
}

+ (id)singletonWithZone:(NSZone*)zone
{
id singleton = nil;

Class class = [self class];
if (class == [FTSWAbstractSingleton class]) {
[NSException raise:NSInternalInconsistencyException
format:@"Not valid to request the abstract singleton."];
}

@synchronized(self) {
singleton = [s_FTSWAbstractSingleton_singletons objectForKey:class];
if (singleton == nil) {
singleton = class_createInstanceFromZone(class, 0U, zone);
if ((singleton = [singleton initSingleton]) != nil) {
[s_FTSWAbstractSingleton_singletons setObject:singleton forKey:class];
}
}
}

return singleton;
}

- (id)initSingleton
{
return [super init];
}

// Disallow the normal default initializer for instances.
- (id)init
{
[self doesNotRecognizeSelector:_cmd];
return nil;
}

// ------------------------------------------------------------------------ ---------------------------------------------
// The following overrides attempt to enforce singleton behavior.

+ (id)new
{
return [self singleton];
}

+ (id)allocWithZone:(NSZone *)zone
{
return [self singletonWithZone:zone];
}

+ (id)alloc
{
return [self singleton];
}

- (id)copy
{
[self doesNotRecognizeSelector:_cmd];
return self;
}

- (id)copyWithZone:(NSZone *)zone
{
[self doesNotRecognizeSelector:_cmd];
return self;
}

- (id)mutableCopy
{
[self doesNotRecognizeSelector:_cmd];
return self;
}

- (id)mutableCopyWithZone:(NSZone *)zone
{
[self doesNotRecognizeSelector:_cmd];
return self;
}

- (unsigned)retainCount
{
return UINT_MAX;
}

- (oneway void)release
{
}

- (id)retain
{
return self;
}

- (id)autorelease
{
return self;
}

- (void)dealloc
{
[self doesNotRecognizeSelector:_cmd];
}
// ------------------------------------------------------------------------ ---------------------------------------------
@end

I use it like following...

@interface FTSWSerialPortRegistry : FTSWAbstractSingleton
....
@end

@implementation FTSWSerialPortRegistry

+ (FTSWSerialPortRegistry*)sharedRegistry
{
static FTSWSerialPortRegistry *s_FTSW_sharedSerialPortRegistry = nil;

@synchronized(self) {
if (s_FTSW_sharedSerialPortRegistry == nil) {
s_FTSW_sharedSerialPortRegistry = [self singleton];
}
}

return s_FTSW_sharedSerialPortRegistry;
}

- (id)initSingleton
{
if (self = [super initSingleton]) {
...
}

return self;
}
...

@end
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

References: 
 >is this enough for a singleton? (for a class cluster "placeholder") (From: Ben Dougall <email@hidden>)
 >Re: is this enough for a singleton? (for a class cluster "placeholder") (From: Shaun Wexler <email@hidden>)

  • Prev by Date: Re: XCode const warnings
  • Next by Date: Re: [Slightly OT] Shareware donation collection
  • Previous by thread: Re: is this enough for a singleton? (for a class cluster "placeholder")
  • Next by thread: NSTableView selection and Drag&Drop
  • Index(es):
    • Date
    • Thread