• 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: Wed, 11 Feb 2004 13:56:25 -0800

On Feb 11, 2004, at 1:22 PM, Ben Dougall wrote:

i got this code from the archives for a singleton object:

+ (id)allocWithZone:(NSZone *)zone {
if ([MyAbstractClass self] == self)
return NSAllocateObject([MyConcreteClass self], 0, zone);
return [super allocWithZone:zone];
}

don't quite understand it -- will that give an ok/perfectly fine singleton implementation? usually the singleton implementations that i've seen involve a static variable in the class, but i know there's a variety of ways to do singletons. is there something else that needs to be done further to the above code, or is that it, so far as the singleton aspect of it goes?

The above looks more about returning a concrete sub-class in place of an abstract class, not a singleton.

This is what I do for a singleton (not all is needed in theory but)...

static FTSWSerialPortRegistry *s_FTSW_sharedSerialPortRegistry = nil;

+ (FTSWSerialPortRegistry*)sharedRegistry
{
@synchronized(self) {
if (s_FTSW_sharedSerialPortRegistry == nil) {
s_FTSW_sharedSerialPortRegistry = [[self alloc] init];
}
}

return s_FTSW_sharedSerialPortRegistry;
}

// -------------- Singleton overrides --------------
+ (id)allocWithZone:(NSZone *)zone
{
@synchronized(self) {
if (s_FTSW_sharedSerialPortRegistry == nil) {
return [super allocWithZone:zone];
}
}

return s_FTSW_sharedSerialPortRegistry;
}

- (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;
}
_______________________________________________
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.

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

  • Prev by Date: RE: Cocoa wizard & gurus, Help!!
  • Next by Date: Re: Cocoa wizard & gurus, Help!!
  • Previous by thread: is this enough for a singleton? (for a class cluster "placeholder")
  • Next by thread: Re: is this enough for a singleton? (for a class cluster "placeholder")
  • Index(es):
    • Date
    • Thread