问题
I´m programming a Class which acts as a Singleton. I was wondering, does it make sense to have non-static properties for this class?
Pseudocode example:
class Foo extends MySingletonClass {
private static string bar;
private string baz;
/* more code here */
}
回答1:
It's not wrong to have static properties, but it's redundant in a singleton.
Also, if you have static properties, and later you need to change the class to not be a singleton no more, you'll need to change the properties either (as every code that access it). So I recommend you not to mark as static, unless really needed.
来源:https://stackoverflow.com/questions/4723245/singleton-class-static-properties-or-non-static-properties