Why is using a Global Variables class (singleton) bad practise?

This question already has an answer here:

  • What is so bad about singletons? [closed] 36 answers

  • A lot of objects in UIKit are singletons. The UIApplication object is a singleton. NSUserDefaults has the standardUserDefaults singleton. UIDevice currentDevice returns a singleton.

    Just because it's a singleton doesn't mean it's bad. What is bad is when you start tying functionality in other classes to your singleton object, with it so deeply ingrained that you can't alter the singleton or the affected object easily.

    I use a singleton to store my non-CoreData object structures. I also define some helper methods for getting the library directory, encoding data, and keyed archiving. So I can reference a master array object wherever I need it, or easily access methods that would otherwise just be copy and pastes.


    Globals.m doesn't sound really like a singleton, and if it is, it looks like it's just used for configuration purposes? Then a file full of #defines or extern would do the same, I guess. Without any more details it's hard to know what you're searching.

    链接地址: http://www.djcxy.com/p/82162.html

    上一篇: 为什么Singleton是比全局对象更好的选择

    下一篇: 为什么使用全局变量类(单例)不好的做法?