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,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
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.