<story><title>So Singletons are bad, then what?</title><url>http://programmers.stackexchange.com/questions/40373/so-singletons-are-bad-then-what</url></story><parent_chain><item><author>shitgoose</author><text>Hilarious! Doing x=C.getX() is bad, but if it is hidden by numerous layers of libraries and monstrous config files, somehow it becomes acceptable. Out of site, out of mind. The fact that global scope bean is essentially a singleton, doesn&amp;#x27;t seem to bother architecturally inclined crowd - they are too busy admiring sound of their own voice pronouncing words &amp;quot;dependency injection&amp;quot;, &amp;quot;mutability&amp;quot; and &amp;quot;coupling&amp;quot;.&lt;p&gt;The top answer is a perfect example of what is wrong with IT today. It takes a working solution, declares it wrong and starts piling up classes and interfaces to solve a problem, that was never a problem in first place (OP never said that their singleton-based cache didn&amp;#x27;t work, he merely asked if there are &amp;quot;better&amp;quot; ways of doing it). So in the end we have the same singleton cache, but hidden behind interfaces (&amp;quot;It makes the code easier to read&amp;quot; - yea, right, easier, my ass! Ctrl+click on interface method and try to read the code),  thousand lines xml Spring configs, and other crap that is completely irrelevant, hard to follow and debug, but glamorous enough for SOA boys to spend endless hours talking about it.</text></item></parent_chain><comment><author>Sandman</author><text>Doing x=C.get(x) is bad because there&amp;#x27;s no way of mocking C, thereby making unit testing a component that uses C impossible (the unit test will need to use the concrete implementation of C). Using dependency injection as described in the accepted answer, and particularly separating the concerns of C by using several different interfaces allows you to not only mock C, but actually create mock classes each of which is mocking one particular logical subset of C&amp;#x27;s functionalities.&lt;p&gt;It&amp;#x27;s easy to dismiss all this as &amp;quot;bloated&amp;quot; and &amp;quot;enterprisey&amp;quot; and people that use this as &amp;quot;architecture astronauts&amp;quot; but in reality, this pattern really does help. Well, at least if you want to be able to unit test your code properly. Otherwise, you might as well just use a singleton object.</text></comment>
<story><title>So Singletons are bad, then what?</title><url>http://programmers.stackexchange.com/questions/40373/so-singletons-are-bad-then-what</url></story><parent_chain><item><author>shitgoose</author><text>Hilarious! Doing x=C.getX() is bad, but if it is hidden by numerous layers of libraries and monstrous config files, somehow it becomes acceptable. Out of site, out of mind. The fact that global scope bean is essentially a singleton, doesn&amp;#x27;t seem to bother architecturally inclined crowd - they are too busy admiring sound of their own voice pronouncing words &amp;quot;dependency injection&amp;quot;, &amp;quot;mutability&amp;quot; and &amp;quot;coupling&amp;quot;.&lt;p&gt;The top answer is a perfect example of what is wrong with IT today. It takes a working solution, declares it wrong and starts piling up classes and interfaces to solve a problem, that was never a problem in first place (OP never said that their singleton-based cache didn&amp;#x27;t work, he merely asked if there are &amp;quot;better&amp;quot; ways of doing it). So in the end we have the same singleton cache, but hidden behind interfaces (&amp;quot;It makes the code easier to read&amp;quot; - yea, right, easier, my ass! Ctrl+click on interface method and try to read the code),  thousand lines xml Spring configs, and other crap that is completely irrelevant, hard to follow and debug, but glamorous enough for SOA boys to spend endless hours talking about it.</text></item></parent_chain><comment><author>bd_at_rivenhill</author><text>I&amp;#x27;ve never really understood the point of most of the Spring usage I&amp;#x27;ve seen; seems like most people want to replace Java code that is type-checked and can throw exceptions at reasonable places when something goes wrong with XML &amp;#x27;code&amp;#x27; that has neither of these properties and thus turns debugging into a black art.  Apparently, this is the right way to do things.&lt;p&gt;The only reasonable use case I&amp;#x27;ve encountered in practice is the ability to replace some major components of your program with stubs for use in automated testing.</text></comment>