Singleton class: static properties or non-static properties?

£可爱£侵袭症+ 提交于 2019-12-23 20:51:03

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!