views:

153

answers:

3

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
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
+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