4 ms·
Agree 1000%. Haven't used once since 2002 since I started using di containers.
by smrtinsert 12y ago
Agree 1000%. Haven't used once since 2002 since I started using di containers.
- cpitman 12y agoExactly, the "Singleton" pattern should only be used when having two instances of one object will be catastrophic. In all other situations where you only think you want one of a class, dependency injection handles that. A good DI container will even keep track of your pseudo-singleton for you. Then unit testing becomes feasible again, without having to hack around that Class X is using the "ConnectionManager" to connect to the database behind your back.
- dkersten 12y agoI always laughed when people said "yeah, but, why would you ever want two ConnectionManagers? What if you create more than one accidentally! Make it a singleton and that can't happen!" My reply was along the lines of "when was the last time you accidentally wrote two for loops when you wanted only one? If you don't want more than one ConnectionManagers then don't create more than one" In my opinion its very rare that you need an object that is both globally accessible and you must not have more than one.