Re: Automagic instantiation of singletons? (Christian Brunschen)
Re: Automagic instantiation of singletons? (Christian Brunschen)
- Subject: Re: Automagic instantiation of singletons? (Christian Brunschen)
- From: Chris Hanson <email@hidden>
- Date: Wed, 23 Nov 2005 00:23:16 -0800
On Nov 22, 2005, at 11:14 PM, Greg Herlihy wrote:
As already noted, it is possible to implement a similar model in
Objective C
by defining a class method that returns the Singleton object,
instantiating
it if necessary. Provided that other dependent singletons follow
this model,
there should be no order-of-construction problems. And in fact some
Foundation objects already follow this approach: NSApplication's
sharedApplication is one example.
The above is the pattern I would suggest for implementing singletons
in Objective-C.
I would further suggest that developers should generally not go out
of their way to ensure that a singleton is "truly" a singleton, by
overriding -retain, -release, -autorelease, -init, etc. Being able
to directly instantiate objects of a class -- even if doing so isn't
part of the class's API contract -- makes it much easier to write
effective unit tests involving that class.
-- Chris
_______________________________________________
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