On Mar 27, 2008, at 3:44 PM, Justin Giboney wrote:

I need to create a series of classes that implement the Singleton design pattern. These classes have a lot of similar methods (I am trying to create a series of DAOs see: http://en.wikipedia.org/wiki/ Data_Access_Object).

I was thinking that it would be best to create a super class, and a series of subclasses to that super class. The problem I am running upon is that the Singleton pattern requires a static variable.

How can I get a variable that is static to each subclass, but that is declared in the super class?

Objective-C doesn't support the notion of class variables. The convention is to use static variables, which have their usual C semantics. That is, they are not "static to a class", they are static to the compilation unit.

You're going to need a static variable in the implementation file of each subclass. It may be possible to pass a pointer to that variable to the superclass implementation at a strategic point so you can still share code, but this strikes me as more complicated than necessary.

Also, I'm aware that there are some implementations of the basic Singleton functionality in a reusable forms. You can search the list archives for them. I don't remember them off-hand. Here's one: http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/ Foundation/GTMObjectSingleton.h

You're probably familiar with this, but I thought I'd add it for completeness: http://developer.apple.com/documentation/Cocoa/ Conceptual/CocoaFundamentals/CocoaObjects/chapter_3_section_10.html

Cheers,
Ken

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to