• 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: glenn andreas <email@hidden>
  • Date: Thu, 1 Dec 2005 11:46:52 -0600


On Dec 1, 2005, at 11:27 AM, David Gimeno Gost wrote:

On 30 Nov 2005, at 02:11, Jeff Laing wrote:

I think whats being suggested by about 30% of the participants in this
thread is that the singleton register itself to listen for the
notifications.

I believe David is *not* suggesting this, but rather that your application
logic listen for the notification and call an appropriate method on the
singleton to tell it to go away. (Or perhaps have the application delegate
do it - either approach would work)

Actually I've always had both approaches in mind and I've strived to not make this explicit until now on purpose. This issue is tough if you want to consider it from a general point of view, and the level of misunderstanding in this thread already was high enough.

The real issue here is possible cleanup dependencies. Resources may need to be disposed of in a certain order. An object managing certain resources may depend on the services provided by another object to do its own cleanup.

Then don't use singletons.

Looking through the GOF "Design Patterns" under "Singletons", it's all about "ensure a class has only one instance, and provide a global point of access to it." There is absolutely nothing in the singleton pattern that deals with cleanup, deallocation, etc, and, in fact, in the canonical C++ implementation of Singleton in said book (p 129) does not have a destructor, because it isn't ever deleted.


So you're worrying about how a Cocoa singleton would handle resource releasing upon deallocation - it would work exactly like the canonical C++ implementation (i.e., it doesn't do anything, because there is no explicit support for deallocating the singleton).

You're looking for Singleton + RAII which is a different beast from an Objective-C Singleton, since RAII requires deterministic destructors (which neither the Singleton pattern as defined, nor Objective C in general, have).


Glenn Andreas email@hidden
<http://www.gandreas.com/> wicked fun!
quadrium | build, mutate, evolve | images, textures, backgrounds, art

_______________________________________________
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: David Gimeno Gost <email@hidden>
References: 
 >Re: Is Apple's singleton sample code correct? (From: David Gimeno Gost <email@hidden>)

  • Prev by Date: Re: Is Apple's singleton sample code correct?
  • Next by Date: Re: [NSApp mainMenu] returns nil under 10.4.3
  • 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