Re:Why Code Snobs Are Invaluable
by Retra on Thursday February 26, 2015 @03:06PM
(#9115099)
I wrote a singleton the other day. Using Java, I had a static utility class that had a function that I wanted to memoize. So I put a static HashMap in the class to do the lookups. There's no reason to have multiple copies, and it's easy to instantiate it only if it is needed.
[ Parent
| on HN ]
Re:Why Code Snobs Are Invaluable
by thedudemabry on Thursday February 26, 2015 @03:20PM
(#9115200)
That's reasonable, but it seems to be equivalent to creating a top-level function and associated global variable in other languages. Java's prohibition of those constructs necessitated the class wrapper, but doesn't appear to provide any benefits over another more generalizable method, namespacing.
That's true. If I weren't using java, I wouldn't have had to use a singleton. That's as much of a problem with singletons as it is an over-reliance on the object-oriented style.
I suspect dependency injection has the same problem: you could achieve the same results in a non-object-oriented language.