开发者

What do you call a "singleton" that allows a public constructor for additional instances?

I'm making a class that supports the Singleton use (one instance is available), yet it also supports normal instance use (via a public constructor).

If you only want one, use that one. If you want 5, new them up.

I clearly can't call this singleton,开发者_高级运维 or some other dev will come along and make my constructor non-public. What can I call this to indicate how it is to be used? Naming is hard.

Some guesses for your amusement: "StaticallyAvailable"? "ThreadReady"? "SingletonOptional"?


It sounds like you've got a "default instance". You could name your object along those lines.


Usually the point of a singleton is for an object that has an unique instance. If multiple copies of the class can exist, then it means that "oneness" is not a necessary behavior. This is just like any other object.

Probably, the reason why you code it as a Singleton is for the global access. Just call it a global object then.


A Pool ?

(esp if you know how about the instance count early, but want to encapsulate some of the behaviour of constructing / destroying)


This does not sound like a singleton to me, it sounds like a regular class that always has at least one instance. Calling this a singleton, and writing the code as though it was one (eg. private static MySingleton instance = new MySingleton();) is confusing - decouple the "always present" instance from your class definition somehow. Make it clear in your implementation that this is a normal class that just happens to always have at least 1 instance around.


You could create a default instance getter.

private TheClass defaultInstance;
public Default{ get{ return defaultInstance??(defaultInstance = new TheClass());}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜