Views: 100.6K
Replies: 7
Archived
|
Difference between Singleton and a static classHello All,
Actually, I read a lot about the Singleton Pattern and static classes, but never quite understood the difference. Please help. Anshuman Saini Anshuman Saini, Aug 26, 2010
this is a wrong answer.
you mean to say static class and singleton class has no difference.
Then why on earth singleton exists if there is already static class and both are same according to you.
Apr 17, 2012
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.
Apr 06, 2011
|
|
Reply 1Ravi Kumar, Jul 16, 2014
|
|
Reply 2Hey, 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 Rajesh Shanmugam, May 31, 2011
|
|
Reply 3It is nice question. these are some differences:
1. Singlton class can implement interfaces. 2. We can pass singlton object as parameter. Shashi Jha, May 28, 2011
|
|
Reply 4Singleton 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 Shakeel Khan, Apr 06, 2011
|
|
Reply 5Hello.. 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. Rajesh Pillai, Sep 03, 2010
|
|
Reply 6Singleton = 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 Kaushal Patel, Aug 28, 2010
|
|
Reply 7Take a look at this previous thread...http://dofactory.com/topic/1001/singleton-as-a-static-class.aspx
Robert Blixt, Aug 26, 2010
|