FROM : Ken Thomases
DATE : Thu Mar 27 22:01:05 2008
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
DATE : Thu Mar 27 22:01:05 2008
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
| Related mails | Author | Date |
|---|---|---|
| Justin Giboney | Mar 27, 21:44 | |
| Ken Thomases | Mar 27, 22:01 | |
| Hamish Allan | Mar 27, 22:05 | |
| Troy Stephens | Mar 27, 22:12 | |
| Jon Gordon | Mar 28, 02:47 |






Cocoa mail archive

