• 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 Apple's singleton sample code correct?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Is Apple's singleton sample code correct?


  • Subject: Re: Is Apple's singleton sample code correct?
  • From: Shaun Wexler <email@hidden>
  • Date: Thu, 1 Dec 2005 19:02:32 -0800

On Dec 1, 2005, at 5:38 PM, mmalcolm crawford wrote:

On Dec 1, 2005, at 9:27 AM, David Gimeno Gost wrote:
<snip a bunch of newbie crap>

Please describe and show an implementation of the solution you propose. The current document basically describes two patterns (a "simple" singleton and a "true" singleton) in about three short paragraphs and 20 so lines of code. You've written many times this amount in the thread. It should be trivial, then, to provide a solution that:

Is easy to understand
Is simple to implement
Allows dealloc to be called
Ensures that dealloc is not called prematurely (to avoid expensive re-instantiation)
Prevents access of a freed object
Allows clients to abide by Cocoa's memory management rules
Allows the singleton to be accessed from any part of the application, without assuming an application architecture beyond that supplied by Cocoa
Does not impose a particular application architecture on the client

(Note: The current pattern meets all of these, with the exception of "Allows dealloc to be called" -- this is deemed to be irrelevant since it is argued that you should use a different pattern for resource clean-up anyway...)

If you can do this, it can then be subjected to proper scrutiny and the debate can move forward in a useful and constructive fashion. Until then it's like tilting at ethereal windmills...

@interface MortalSingleton : NSObject
@end

// ======================================================================== =======================
//
// MortalSingleton.m
//
// Created by Shaun Wexler on Thu Dev 01 2005.
// Copyright (c) 2005 SKW Development. All rights reserved.
//
// ======================================================================== =======================

@implementation MortalSingleton

static NSMutableDictionary *singletons = nil;

+ (void)initialize
{
@synchronized([MortalSingleton class]) {
if (!singletons) {
singletons = [[NSMutableDictionary alloc] initWithCapacity:8];
}
}
}

+ (id)sharedInstance
{
id sharedInstance = nil;

@synchronized(singletons) {
NSValue *singleton;
if ((singleton = [singletons objectForKey:self])) {
sharedInstance = [[singleton nonretainedObjectValue] retain];
} else if ((sharedInstance = [NSAllocateObject(self, 0, NULL) init])) {
[singletons setObject:[NSValue valueWithNonRetainedObject:sharedInstance] forKey:self];
}
[sharedInstance autorelease];
}

return sharedInstance;
}

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

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

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

- (id)copy
{
return self;
}

- (id)copyWithZone:(NSZone *)zone
{
return self;
}

- (id)mutableCopy
{
return self;
}

- (id)mutableCopyWithZone:(NSZone *)zone
{
return self;
}

- (void)dealloc
{
@synchronized(singletons) {
[singletons removeObjectForKey:[self class]];
}

[super dealloc];
}

@end
--
Shaun Wexler
MacFOH
http://www.macfoh.com

Arguing with an engineer is like wrestling with a pig in mud.
After a while, you realize the pig is enjoying it.


_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden
  • Follow-Ups:
    • Re: Is Apple's singleton sample code correct?
      • From: Greg Titus <email@hidden>
    • Re: Is Apple's singleton sample code correct?
      • From: mmalcolm crawford <email@hidden>
References: 
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)
 >Re: Is Apple's singleton sample code correct? (From: mmalcolm crawford <email@hidden>)

  • Prev by Date: Re: CoreData, inheritance and plug-ins
  • Next by Date: Re: Is Apple's singleton sample code correct?
  • Previous by thread: Re: Is Apple's singleton sample code correct?
  • Next by thread: Re: Is Apple's singleton sample code correct?
  • Index(es):
    • Date
    • Thread