How Bad Are Singletons?

后端 未结 7 2026
傲寒
傲寒 2021-01-02 22:41

So ....

There are obviously many questions that all have been asked about Singletons, Global State Variables, and all that great stuff. My question is,

7条回答
  •  栀梦
    栀梦 (楼主)
    2021-01-02 23:24

    In the process of cleaning a Singleton would represent hiding the dirt in the dark corner instead of cleaning.

    It's so widely used because it does it's job of hiding problems very well.

    It's a good thing for those who are to clean, but don't want to.

    It's a bad thing for those who are up to actually clean something.

    Consider Dependency Injection http://martinfowler.com/articles/injection.html if a Singleton is causing a problem you can't hide anymore.

提交回复
热议问题