• 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: Singletons?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Singletons?


  • Subject: Re: Singletons?
  • From: Charles Jolley <email@hidden>
  • Date: Fri, 21 Jun 2002 03:22:32 -0500

I think you mean declaring a static in the .m and providing access to it through a class accessor method. This is the "Cocoa" way of doing a singleton. As long as your static variable is not exported through the header file, someone would have to be very deliberate to change the contents of your static.

Anyway, you're not much safer any other way since someone could simply override your class accessor method using a category or a subclass posing as your class. In fact, if someone had only a binary of your class and the header file, this latter approach would be easier than tracking down a static variable.

On the flip side, sometimes you _want_ someone to be able to change the singleton instance so they can replace your instance with an "upgraded" version of their own. Cocoa does not have a standard pattern for doing this. Some classes, such as NSFontManager, allow you to designate a factory class to create the singleton. Others will use the first instance of the class or any subclass.

As for the namespace part...well, yeah, that is annoying. Objective-C as a whole doesn't having the whole namespace management thing down very well, IMHO. I would really like to have a Java-like packages system instead of having to use the two letter prefixes.

Cheers,
-C

On Friday, June 21, 2002, at 02:43 AM, The Amazing Llama wrote:

Okay, so ObjC has no static variables.

How do you do a Singleton? What I'm seeing in code on the 'net is to make a global static variable in the .m, but this seems dangerous (someone could change it out from under you, and you can't do anything to stop them) and not at all object oriented (it's just floating out there, polluting the global namespace).

There has to be a better way... right?

-The Amazing Llama <tallama at mac dot com>
"Life is like an exploded clown. It's really funny until you figure out what just happened."
_______________________________________________
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.
_______________________________________________
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.

References: 
 >Singletons? (From: The Amazing Llama <email@hidden>)

  • Prev by Date: Singletons?
  • Next by Date: Re: Singletons?
  • Previous by thread: Singletons?
  • Next by thread: Re: Singletons?
  • Index(es):
    • Date
    • Thread