Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Advantage of Static class over use of Singleton

Duplicate

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?

like image 949
Peanut Avatar asked May 08 '09 11:05

Peanut


People also ask

What if I use static instead making singleton?

It is not possible to inherit from a static class, while it is possible with singleton pattern if you want to allow it. So, anyone can inherit from a singleton class, override a method and replace the service. It is not possible to write an extension method to a static class while it is possible for a singleton object.

What is the difference between using a static class and a singleton pattern?

The difference between the Singleton and Static is Singleton Class can have value when Class object instantiated between server and client, such a way if three client want to have a shared data between them Singleton can be used. Static are always just shared and have no instance but multiple references.

Why static is used in Singleton design pattern?

Singletons have a static property that you must access to get the object reference. So you are not instantiating an object such as in the manner we do for a normal class.

What is the disadvantages of using singleton?

Singletons hinder unit testing: A Singleton might cause issues for writing testable code if the object and the methods associated with it are so tightly coupled that it becomes impossible to test without writing a fully-functional class dedicated to the Singleton.


3 Answers

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

like image 88
Roman Avatar answered Oct 21 '22 17:10

Roman


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.

like image 35
Preet Sangha Avatar answered Oct 21 '22 19:10

Preet Sangha


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.

like image 3
Patrick McDonald Avatar answered Oct 21 '22 19:10

Patrick McDonald