Singleton 类:静态属性还是非静态属性?
我正在编写一个充当单例的类。我想知道,此类具有非静态属性是否有意义?
伪代码示例:
class Foo extends MySingletonClass {
private static string bar;
private string baz;
/* more code here */
}
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 */
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(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.