Possible Duplicate:
Advantage of Static class over use of Singleton
Static class vs. Singelton - what are the pros & cons of each one?
Possible Duplicate:
Advantage of Static class over use of Singleton
Static class vs. Singelton - what are the pros & cons of each one?
Static methods and singletons end up being basically the same thing. Static methods' "singleton instances" are the type.
Both of these should be used with caution and minimized. They create dependencies in code that is hard to remove for testing.
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.