Singleton class vs static methods and fields?

This question already has an answer here:

  • Difference between static class and singleton pattern? 36 answers

  • This is mainly due to the limitations of static types versus singletons . Which are:

  • Static types cannot implement interfaces and derive from base classes.
  • From the above we can see that static types cause high coupling - you cannot use other classes in tests and different environments.
  • Static classes cannot be injected using dependency injection.
  • Singletons are much easier to mock and shim.
  • Singletons can be easily converted to transients.
  • These a few reasons from the top of my head. This is probably not all.

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

    上一篇: 单一类或只有静态字段的类?

    下一篇: 单身类与静态方法和字段?