Reputation: 19407
What’s wrong with singleton?
Singletons: good design or a crutch?
Singleton: How should it be used
What is so bad about Singletons
You can find numerous reasons for using a Singleton over a Static class. But there must surely be some situations where it is better to use a static class before a Singleton. What are they?
Upvotes: 8
Views: 6953
Reputation: 42246
A singleton is a class of which only one instance can be instantiated, whereas there is no instance associated with a static method.
If you can implement the function you want with a single static method, then that is probably your best approach, because it is easier to implement. Consider extension methods - they are just static methods with syntactic sugar. If you can logically view the static method as a helper to an existing class, then it makes sense to use a static method.
On the other hand, if there is some sort of state involved in the functionality you are trying to achieve, then it is probably best to use a Singleton instead. The Singleton object can contain/manage its state and manage concurrent access/threading, whereas this becomes much more complicated with static classes and static methods. If you are using Singleton's in C#, I highly recommend reading Jon Skeet's article on proper Singleton implementation, which is available at http://www.yoda.arachsys.com/csharp/singleton.html .
Singleton's are more comparable to static classes than static methods. A big advantage that singletons have in this comparison is that they can implement interfaces and derive from base classes. This allows you to decouple their implementations from their interfaces. For example, if I have an interface IAccountService
in my core assembly with a Singleton implementation, SingletonAspNetAccountService
in my service layer, then I can inject the IAccountService
into my UI layer with an IoC container, without requiring a dependency on my service layer in the UI layer. On the other hand, if I had a static Accounts
class, then I would have to either create an adapter to the static class's methods or have a dependency on the service layer in my UI in order to access the static account functionality.
Upvotes: 0
Reputation: 66156
You can use static class when:
1) all its methods are utilities (nice example - class Math)
2) you don't want to deal with preserving your instance from garbage collector (in applets), but I would better use singleton there
3) you are absolutely sure that it wouldn't become stateful in the future and you are sure that you will always need only one instance of that class
If you are using singleton and in one moment you realize that you need several instances then your singleton easily can be transformed to multitone, but you'll have a problem with static class
Upvotes: 9
Reputation: 65466
Having fought with the testability of consumers of static classes over the years I can honestly say that they are the work of evil minds. Seriously though, I'd use static classes for extention methods in C# but not really anywhere else.
Upvotes: 5
Reputation: 65411
If your class doesn't store any state, then use a Static class.
If it stores state and you require a single instance, then (maybe) use a Singleton.
Otherwise use a regular class.
Upvotes: 3
Reputation: 26722
Static class is better for when you don't need to change the implementation. With a Singleton, you can have an interface with various implementations. A Static class, can only be an implementation.
Upvotes: 2
Reputation: 19313
It's always where you don't actually need to pass the singleton instance anywhere. For example, singleton will be useful if it implements some interface, you can't do it with a static class.
Remember, every Class instance is a singleton, managed by JVM. So static class is a singleton.
Upvotes: -2