Re: Is Apple's singleton sample code correct?
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