Difference between Static Class and Singleton class
This is my first blog and as a technical person I want to start with some technical stuff. So here is something technical I have choose, that is "Difference between Static Class and Singleton class".
Recently I went through with the Singleton class. Static class I already have gone through with, so I found many difference in between them. If you do the google many stuff you will be able to find on this topic but I tried to conclude them in short in my way. So let's begin with the actual differences:
Recently I went through with the Singleton class. Static class I already have gone through with, so I found many difference in between them. If you do the google many stuff you will be able to find on this topic but I tried to conclude them in short in my way. So let's begin with the actual differences:
- Singleton objects are stored in heap but Static objects stored in stack
- We can clone the Singleton object but we can not clone the Static object.
- We can implement interface with the Singleton class but we can not implement interface with the Static class.
- Singleton class allows all kind of methods and properties and other members whether it is public or private or internal or protected but Static class only accept static members.
- Static class can not have constructor other than static one.
- We can not pass Static class object to method as a parameter but we can do that with the Singleton class object.
- We can inherit other class to the Singleton class but same we can not do with the Static class.
- We can dispose the object of Singleton class but not the Static class because ideally we not creating the object of it.
- Singleton class method can be overridden (but here is twist because ideally Singleton class should be Sealed one and in that case it's members can not be a overridden) but Static class methods can not.
- Singleton can be lazy loaded when need where-as Static always loaded.
- Better performance static methods are bonded on compile time.
- Singleton members can be serialized which Static members can not.
- Static class care much easier to implement and Singleton is better if you have to pass object as parameter to any method.
Comments
Post a Comment