开发者

What are good candidates for singleton? [duplicate]

This question already has answers here: Closed 11 years ago. 开发者_StackOverflow社区

Possible Duplicate:

On Design Patterns: When to use the Singleton?

Hi Just wondering what are good candidates for singleton?

Just reading about it and wondering if sometimes I have misused it.

Generally speaking when would you use a singleton?

thanks a lot


Basically, whenever I need a class but am not sure I will need a singleton pattern, I code to the singleton interface, but let the implementing class determine whether or not to return a single instance or a new instance. That way, calling classes don't have to worry about changing if the move to singleton (or the move away from singleton) should occur.

Places I've used this successfully is on my repositories. But I always preface every singleton answer with a 'Make sure you pay attention to thread-safety, whichever way you go'. Sometimes weird things can happen in a multi-threaded environment when dealing with a singleton.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜