开发者

protected access specifiers in singleton class

HI,

In singleton class we are declaring constructors as p开发者_运维技巧rivate. Is it possible to give as protected.

If giving as protected is it beneficial.

Whats the advantage or disadvantage of private over protected


If you make the constructor protected, then any class inheriting from it could instantiate it multiple times. That would no longer make it a singleton.

Before you make a class into a singleton, thing hard if you really need to (do you really have to have a single instance of it in the whole application? Really?).


If you really want to have a singleton you have to avoid that "someone else" can create an instance of the class. That's why it should be private, and the class should be sealed, which makes protected just the same as private since there is no inheritance possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜