tags:

views:

53

answers:

2

I have created a singleton class and I want to create a class which is subclass of this singleton class, what is the correct method to do it

+2  A: 

I don't know about Objective-C in particular, but in general singleton classes should prevent subclassing. If you've got an instance of the base class and an instance of the subclass, then you've effectively got two objects you can regard as instances of the base "singleton" class, haven't you?

As soon as you've got two instances, it's not really a singleton any more... and that's leaving aside the possibilities that there are multiple subclasses, or that the subclass itself allows multiple instances to be created.

Of course you can change your base class so it just has a way of getting at a single "default" instance, but that's not quite the same as making it a singleton.

Jon Skeet
A: 

If Jon didn't convinced you to don't do it you should do it this way:

In your superclass init you singelton instance with [[[self class] alloc] init] so then you always get an instance of the class with which you are calling the sharedInstance method. And you don't have to overwrite the sharedInstance method in your subclass.

 [SuperClass sharedInstance] //-> instance of SuperClass
 [SubClass sharedInstance] //-> instance of Class
V1ru8