Singleton Class or Class with only static fields?

This question already has an answer here:

  • Difference between static class and singleton pattern? 36 answers

  • In object-oriented programming generally you should avoid singletons and utility classes if possible.

    However, if really needed I'd go with utility class without any fields - just static methods. By definition utilities should be rather set of stateless functions. Such are well testable in comparison to untestable singleton (which is done with static field). If you need to keep the state then go towards true objects.

    As stated in the comment, you can have a safe singleton done by dependency injection, without static state.

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

    上一篇: 通过枚举方式单例是懒惰初始化?

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