Singleton usage
Is singleton a design pattern or a design antipattern?
From here http://neugierig.org/software/chromium/notes/, it said 'Globals and singletons are already well-known as a design antipattern, but they have an interesting additional cost.'
But the GoF book said singleton a design pattern.
So can you please tell me when does using singl开发者_StackOverfloweton is NOT okay?
Thank you.
The singleton is a design pattern.
However, since it is one of the simplest, it is also one that is most abused and in that sense, a design anti-pattern.
The difficulties in writing testable code that uses singletons is another reason people consider it to be an anti-pattern.
A singleton should only be used when you must have one instance of the class running in the application space. That is, when you have to guarantee that is can only be one such instance.
精彩评论