使单例具有所有静态字段

Making a singleton have all static fields

我一直在想,既然单例允许用户对一个对象只有一个引用,我们是通过静态方法 getInstance 获得的,为什么我们不能决定将单例中的所有字段都设为静态?

Static members are part of class and thus remain in memory till application terminates and can’t be ever garbage collected. Using excess of static members sometime predicts that you fail to design your product and trying to cop of with static / procedural programming. It denotes that object oriented design is compromised. This can result in memory over flow. Also there are certain disadvantages if you make any method static in Java for example you can not override any static method in Java so it makes testing harder you can not replace that method with mock. Since static method maintains global state they can create subtle bug in concurrent environment which is hard to detect and fix.

因此,通过将每个方法设为静态,我们消除了创建单例 class 的目的,即 节省内存