What's the correct method to subclass a singleton class in Objective -C?

后端 未结 9 915
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-09 18:23

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

9条回答
  •  攒了一身酷
    2020-12-09 18:38

    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.

提交回复
热议问题