0

It seems we always use a sharedInstance class variable to access the Singleton and perform methods on it. But why don't we just make all operations class methods and not have a variable to deal with at all? [SingletonClass uploadFile:(NSFile *)file] instead of [[SingletonClass sharedInstance] uploadFile:(NSFile *)file] (or the Swift equivalent).

What benefits do the variable bring? Or am I just overlooking some very integral concept in Singletons that not having a variable would prevent?

Furthermore, what stops this variable from being deallocated by memory? I know it's only created once, but why doesn't it ever get removed?

Doug Smith
  • 29,668
  • 57
  • 204
  • 388

1 Answers1

1

You create a shared instance if you need to be able to store state. If you can get away with just class methods, that is definitely preferable. The less state you have in your app, especially with singletons, the fewer bugs you will create.

drewag
  • 93,393
  • 28
  • 139
  • 128