Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I always get cranky about people fixating on the difference between a static class and a singleton. A static class is just your language's default implementation of the singleton pattern, it just fails at providing any polymorphism to swap out the object for a new one. Mechanically, they do the same thing. Single global point of access to some effectively-global-state.

Also, the singleton thing (and interfaces everywhere) stinks of YAGNI. Your toolkit can find all references to X.Y, which means you can replace all references to X with a reference to X.Instance.Y. when you need to change a static class to a singleton, do so.

Or just use a dynamic language where there isn't any difference. Seriously, I wish C# offered a "root" static class or global variables or something equivalent so that the singleton pattern was less verbose to implement, because you will need them.



It really depends on the context of what you're trying to do. If you're whipping together something as a test, or if it's code that you _know_ will be thrown away in the future, then cowboy it up and get it done quickly. Good design and quality work takes more time than is needed for many first iterations so YAGNI is right.

The flip side is that if quality does matter for a project, then unit tests are important, and the whole point of Aaronaught's answer is that things like unit tests and refactoring are usually impossible with a traditional singleton pattern.

It's definitely a fine line though. As a wise programmer once said: "Broken gets fixed, but shitty lasts forever." Be careful when you deliberately choose the shitty route - it usually comes back to bite you.


What other languages than C# have the concept of a static class?


Other languages can sport a class full of static methods and a private stub constructor, all C# offers is the means to force a class to only contain static methods and block the constructor.

You can make an all-static class in any OOP language that supports class-level methods and members.

That's my point. This argument is all semantics. There's a million little ways to create something with the functional utility of a Singleton, and there's no sense in pretending that they're vastly different.


It was just idle curiosity - I wasn't familiar with the term "static class", looked it up and wondered if that particular set of features (and the term) were used in any other languages.


Nothing special about it, it was just a cheap feature to add (static was already reserved and not previously added before class) while helping to exemplify a common protocol.

The one nice thing it does provide is access to defining extension methods, which aren't allowed outside static classes.


It's just a class that is both sealed and abstract. Therefore, all of its "methods" must be static.


In ruby, all classes are instances of the Class class. You can put methods on them like any other object.

Yeah.


VB.NET for one. It calls them Modules.


Scala has objects and case objects.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: