>Care to explain what's wrong with singletons?

As others have mentioned, true singletons preclude unit testing IMO. A true 
unit test relies on the fact that one test does not affect another. If one test 
creates any global states, including a singleton, then it necessarily alters 
the starting state for the next test.

I have no problem with something _acting_ like a singleton and ensuring that, 
within a given context, there is only one copy. I just think that enforcement 
should happen via something like dependency injection wherein you can choose 
whether it is truly 'globalish', contextual or that you choose to create a new 
one every time (which is the case when you are testing).

Hope that makes some sense. Honestly, there is nothing wrong with any pattern, 
it is simply that patterns have usefulness within certain confines. A singleton 
is a great way to approach certain applications but begins to get in your way 
when you have multiple modules (especially if you want to unload them), testing 
or areas of your application that need different contexts. This is not the case 
for many, many applications and hence like any other tool, I say use them but 
understand when not to.

Mike

Reply via email to