Singletons are so often said to be a bad design choice, so how should you design an application when you want to avoid them?
+6
A:
you can use concepts such Dependency Injection to inject the services you depend on (basically the singletons you use) to you, instead of providing a global reference to them
Asaf David
2010-04-24 12:42:30
In particular, DI frameworks can simplify a lot of the wiring required, providing the convenience of a singleton, but with better insulation and the possibility to extend to multiple instances in the future, if required.
Dan Bryant
2010-04-24 12:51:42
+2
A:
My 2 cents.
Just design your application in such way that it's realy doesn't matter if it's singleton passed to your object or not.
Consider if you have MySingleton.Instance inside -> that looks bad and you are tightly coupled with this. If you passed MySignleton as a parameter in a method and it's instantiated outside as a MySingleton.Instance or new MySingleton() -> oh well, I still can mock or change it so it doesn't really matter.
Andrei Taptunov
2010-04-24 12:49:29
+1
A:
Check this article: http://www.ibm.com/developerworks/webservices/library/co-single.html
pierocampanelli
2010-05-24 12:17:10