views:

940

answers:

8

In answering this question (http://stackoverflow.com/questions/352317/c-coding-question#352327), it got me wondering...

Is there any danger in regarding a static class as being equivalent to a non-static class instatiation that implements the singleton pattern?

+2  A: 

It isn't exactly equivalent. For example you can pass a reference to a singleton instance as an argument, which you can't do with a static class as there isn't an instance.

What do you mean by "danger"?

Joe
perhaps danger is the wrong term. i was just wondering about differences. i suppose inheritance is an issue too...
spender
A: 

Not sure about C#, but in C++ a static Object will get initialized when it gets initialized, and you have no direct control over that (especially in multithreaded apps). So you need a function to call your object, not just call it directly (unless you want unportable code)

Robert Gould
+7  A: 

The only thing that seems immediately apparent to me is that a static class is basically just a collection of scoped functions (explicitly avoiding "methods" here) and a singleton is still something you can instantiate, even if you can only have 1. 1 > 0.

You can pass a singleton as an argument to something that expects an object of a certain interface, you cannot pass a static class anywhere (except through some reflection trickery)

Kris
+1  A: 

As Robert Gould pointed out, you loose control over construction. You will also get construction issues which are a lot more obscure. Static classes quickly end up with static initializer blocks. These blocks get called the first time someone references your type, and this order may not be as well defined as you like to think. So the run-order of these static initializers may change without you planning so, and can cause strange bugs.

krosenvold
A: 

As Robert said before, the initialization is a main disadvantage of a static class. The static class will usually be initialized lazily, at the last possible moment. However, you lose control over the exact behavior and static constructors are slow.

Often static classes are used to hold global data. And global data creates an implicit dependency between your other objects / classes. So you must be carful when changing this "global object". Can break your application.

TomTom
Actually, unless there's a static constructor (as opposed to static variable initializers), the type will probably be initialized earlier than it needs to be.
Jon Skeet
"Lazy" static constructors is not a problem. You can make static Init procedure manually instead of static constructor.
macropas
My intention was not to say that "lazy" is a problem. An alias-name for the singleton - pattern is "Lazy initialization". What I often see is that Singletons and / or static classes are used as a replacement for global variables.
TomTom
+3  A: 

In many ways, a static class and a singleton are similar. One big difference is that a singleton might implement some interfaces, which isn't possible with a static class. For example, Comparer<T>.Default / EqualityComparer<T>.Default provide (via the interface) the ability to use the item in sorting / dictionary usage.

It is also possible (though tricky) to use a singleton with the standard serialization frameworks. With a static class, you'd have to manage any state persistence manually.

Marc Gravell
A: 

In context of singleton implementation there is no any danger, I think. I often do the same, imlementing singletone via static class. Logically, object reference isn't necessary if it's alone and unique.

macropas
Of course, If there is no necessity in Interface implementation or standard serialization. I agree with Marc Gravell
macropas
+1  A: 

The main danger that I can see with static classes is that they are much harder to mock when writing unit tests. With a singleton you can create it in such a way that you can inject a different class in its place that does test specific functionality, with a static class this is not so easy.

Martin Brown