Why singleton is bad?
2016-06-28 20:55
232 查看
Paraphrased from
Brian Button:
They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a
code smell.
They violate the
single responsibility principle: by virtue of the fact that they control their own creation and lifecycle.
They inherently cause code to be tightly
coupled. This makes faking them out under test rather difficult in many cases.
They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no no for unit tests. Why? Because each unit test should be independent from the other.
Brian Button:
They are generally used as a global instance, why is that so bad? Because you hide the dependencies of your application in your code, instead of exposing them through the interfaces. Making something global to avoid passing it around is a
code smell.
They violate the
single responsibility principle: by virtue of the fact that they control their own creation and lifecycle.
They inherently cause code to be tightly
coupled. This makes faking them out under test rather difficult in many cases.
They carry state around for the lifetime of the application. Another hit to testing since you can end up with a situation where tests need to be ordered which is a big no no for unit tests. Why? Because each unit test should be independent from the other.
相关文章推荐
- .NET c# 单体模式(Singleton)
- php实现singleton()单例模式实例
- 深入理解线程安全与Singleton
- .Net 单例模式(Singleton)
- Python下singleton模式的实现方法
- Python使用metaclass实现Singleton模式的方法
- Serialize/Unserialize破坏单例
- 可序列化单例模式的遗留问题答案
- 在spring来管理实例对象prototype和singleton的选择.针对action如何使用
- 一般Singleton模式通常有几种种形式
- 单例分享(循环引用及内存占用解决)
- 设计模式之单例模式(Singleton)
- 单例模式
- Java设计模式 创建模式-单态模式(Singleton)
- 设计模式 —— 单例模式, Singleton Patttern
- Spring3之Bean的作用域
- 单例模式
- 设计模式-创建型:单例模式(1)
- 设计模式-创建型:单例模式(2)
- Java单例设计模式