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.
精彩评论