本文介绍了辛格尔顿反模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
的结果
的
我听到了很多这一点,但嚣找不到它坚定的原因。
I hear a lot of this but din't find firm reason for it.
Avoid the singleton anti-pattern and replace it with DI.
但是,为什么呢?
but, why?
推荐答案
状态单身更难以进行单元测试。
Stateful singletons are much more difficult to unit test.
我使用无状态的单身人士,我看不出有问题。
I use stateless singletons which I don't see a problem with.
由于单身人士可以实现接口,它们可以使用依赖注入传递(也应该这样在可能被传递)
Since singletons can implement interfaces, they can be passed using dependency injection (and should be passed as such where possible)
这篇关于辛格尔顿反模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!