5 ms·
It starts with the Singleton and vaguely hints that it has some drawbacks. Why start with that, then? And why not flat our recognise that it is understood more
by jmartinpetersen 5y ago
It starts with the Singleton and vaguely hints that it has some drawbacks. Why start with that, then? And why not flat our recognise that it is understood more as an anti-pattern these days?
I don't get blogs like these. This story has been told a million times with more or less the same content and structure.
- nuerow 5y ago> It starts with the Singleton and vaguely hints that it has some drawbacks. Why start with that, then? And why not flat our recognise that it is understood more as an anti-pattern these days? There might be significant tradeoffs with singletons, but I wouldn't go as far as claiming they are an anti-pattern. Far from it. Managing and sharing a single instance across the whole application is a critical part of other basic software architecture constructs like dependency injection or even handing app settings.
- valenterry 5y agoYou misunderstood the singlenton pattern. It's not about "Managing and sharing a single instance", it's about only being able to instantiate one instance of a class. That's not the same, because you can achieve the former without having the latter. I agree that it is an antipattern.
- nuerow 5y ago> You misunderstood the singlenton pattern. It's not about "Managing and sharing a single instance", it's about only being able to instantiate one instance of a class. Not quite, you're confusing a particular way a singleton is implemented with the whole purpose of a singleton and why there's a need to restrict a class to have a single instance to begin with. To illustrate your misconception, you can instantiate a regular class as a global variable and specify that no other instance is allowed in a project in the project's coding standards to enforce this at the code review level, and you still have a singleton. The whole point of a singleton is that only one instance of a class is used across the system, regardless of whether this is enforced by any technice or not. Don't confuse a specific implementation with the purpose, and requiring/expectinv only one instance of a class to be available is obviously not an anti-pattern.
- valenterry 5y ago> Not quite, you're confusing a particular way a singleton is implemented with the whole purpose of a singleton and why there's a need to restrict a class to have a single instance to begin with. Have you actually read the definition(s) of the singleton pattern? E.g. Wikipedia: "In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance." (https://en.wikipedia.org/wiki/Singleton_pattern https://en.wikipedia.org/wiki/Singleton_pattern) And it's the same in most other definitions. Also, make sure to read the criticism on the Wikipedia page - I think the criticism there is enough to classify it as an antipattern, because again, it's not about just having one instance of a type/class.
- nuerow 5y ago> Have you actually read the definition(s) of the singleton pattern? E.g. Wikipedia: "In software engineering, the singleton pattern is a software design pattern that restricts the instantiation of a class to one "single" instance." Yes, I did. I also know what a singleton is, and the whole point it's a thing. Did you read my explanation were I pointed out your misconceptions? > Also, make sure to read the criticism on the Wikipedia page - I think the criticism there is enough to classify it as an antipattern, You should first take your own advice into consideration and first read your sources before citing them. If you did before succumbing to your appeal to authority, you would have noticed the fact that your whole argument that singletons supposedly were an anti-pattern is what, and I quote, "it is frequently used in scenarios where it is not beneficial" because of the global state. Do you understand the problem with the way you're trying to generalize an assertion, specially given that enforcing a global state is often the whole point of using a singleton? If you fail to understand how/why/when a technique is used, that does not make it an anti-pattern. Lastly, please get acquainted with HN's guidelines on commenting as your last comment goes against a few principles stated in them.
- valenterry 5y agoWe are talking about design patterns here, about the singleton design pattern. And not just about singleton objects. You still seem to mix up the two things. > Lastly, please get acquainted with HN's guidelines on commenting as your last comment goes against a few principles stated in them. I think you are just misunderstanding me (and the OP) and I'm trying to clarify. I don't see which principle I would violate with that.
- kubb 5y agoProbably because it's the most well known and understood designa pattern. The second one is probably Factory.
- Cthulhu_ 5y ago> I don't get blogs like these. This story has been told a million times with more or less the same content and structure. Yes, but things like this need to be repeated every few years to show it to a new generation of coders. 30-40 some year olds of today have probably been exposed to the original design patterns book already, but a younger generation grew up and worked with different tech, e.g. ruby / python / php, where these design patterns were reinvented. I mean Ruby on Rails and some PHP frameworks from the 2000's / 2010's reinvented the MVC framework / design pattern, I had never heard of it before until these came back with it, but it's a pattern from the 70's apparently.