Difference between Singleton and a static class

 
50   50
Aug 26, 2010
 
Hello All,

Actually, I read a lot about the Singleton Pattern and static classes, but never quite understood the difference.

Please help.
Anshuman Saini
 1 comment
 
In simple words, singleton is a pattern and not a keywork vs. static is keyword for .Net language. You can create singleton classes any modern day object oriented language. Use static class in c# code and you have singleton code ready for your work. Depending on how you initialized object it becomes thread safe or unsafe. --- Saurin Travadi  Apr 06, 2011



1,128   99.9
Aug 26, 2010
 1 comment
 
Thanks for replying :) I didnt get so much idea about diffrecnce between these . Can you explain in detail. Thanks a lot in advance --- Anshuman Saini  Aug 26, 2010

85   96.0
Aug 28, 2010
Singleton = Single means single object across the application life cycle & the scope of singleton is at application level.
Static=  Static an not have any object pointer & the scope of static is at App Domain level.

I hope this will make your doubt clear.

--
Kaushal
http://kaushalp.blogspot.com
 1 comment
 
Hello Kaushal Thanks for this, but tell me one thing which I can do from the singleton that I can do same thing from the static class. Then what is the use of a static class? --- Anshuman Saini  Aug 31, 2010

1,251   100.0
Sep 03, 2010
Hello.. In addition to responses already provides here are my two cents why would you use a Singleton in favor of static class.

As you know both by now Singleton and Static classes both provide a central point of access  i.e. there's only one instance and one way to access it.

The benefit of Singleton class is it is much more flexible that static classes.  Static classes once defined could not accomodate any future design changes as by design static classes are rigid and cannot be extended.

Singleton on the other hand provides flexibility and also provides sort of a mechanism to control object creation based on various requirements.  They can be extended as well if need arises.  In other words you are not always tied to a particular implementation.  With Singleton you have the flexibility to make changes as when situation demands..


Hope this add some more clarity to the thoughts about its usage.



70   95.3
Apr 06, 2011
Singleton class maintains state. It is thread safe. It gives flexibility to change the logic inside class methods while preserving interfaces by subclassing and returning the subclass object.

public SomeClass {

public static int doSomething(int a, int b) {
return a + b;
}
}

public class AnotherClass {
private AnotherClass instance = new AnotherClass();

private AnotherClass() { // private constructor so no one else can instantiate objects }

public static AnotherClass GetInstance() {
return instance; // classic singleton
}

public int doSomething(int a, int b) {
return a + b;
}
}

There is no functional difference between the following:
int x = SomeClass.doSomething(1,2);
int y = AnotherClass.GetInstance().doSomething(1,2);

Personally, I feel the second construct (what you call "singleton") is more flexible because it hides object management and subclassing from the application layer. For example, I can make the following changes rather easily:

// Converts from singleton to factory
public static AnotherClass GetInstance() {
return new AnotherClass(); // factory method, returns a new object instance each time
}

// Change the implementation altogether
public static AnotherClass GetInstance() {
return new AnotherClassSubclass(); // abstract factory method, returns a new implementation altogether
}

public class AnotherClassSubclass extends AnotherClass {
public int doSomething(int a, int b) {
return a * b;
}
}

Both are very significant changes but we can effect them WITHOUT changing the code.
int y = AnotherClass.GetInstance().doSomething(1,2); // this line of code still works whether you use singletons, factory methods, or abstract factory methods

If you wanted to accomplish the same thing using static methods, you would definitely have to change the code
SomeClass s = new SomeClass(); // explicitly declare a new instance
int x = s.doSomething(1,2);

or
int x = SomeClass.doSomethingDifferent(1,2); // add another static method to multiply instead of add

or
int x = SomeOtherClass.doSomething(1,2); // add another class altogether if you want keep the same method signature and multiply instead of add

 1 comment
 
Here's a simple rule for usage: If your design calls for inheritance and variation then you must use a Singleton and not a Static class as you cannot use inheritance with static classes, by definition. In most usages, you would want your Singleton to implement an interface, something you cannot do with a static class. --- Saleh Najar  Apr 11, 2011

50   50
May 28, 2011
It is nice question. these are some differences:
1. Singlton class can implement interfaces.
2. We can pass singlton object as parameter.

50   50
May 31, 2011

Hey,

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.

Thank you,

Rajesh