Difference between singleton class and static class

Posted by Kumar_jay99 under ASP.NET on 5/17/2011 | Points: 10 | Views : 42374 | Status : [Member] | Replies : 10
what is the difference between singleton class and static class




Responses

Posted by: Kumar_jay99 on: 5/17/2011 [Member] Starter | Points: 25

Up
0
Down
Static classes and singletons both provide sharing of redundant objects in memory, but they are very different in usage and implementation.

Static Class - You can not create the instance of static class.
Singleton pattern - you can create one instance of the object and reuse it.
Static classes- are loaded automatically by the .NET Framework common language runtime (CLR) when the program or namespace containing the class is loaded.
Singleton instance is created for the first time when the user requested.
Static Class class cannot have constructor.
singleton class can have constructor.

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lokesh76 on: 5/17/2011 [Member] Starter | Points: 25

Up
0
Down
Good post. can you please put more light why we go for singleton pattern over static class as we can achieve same thing with static class also.

Thanks
Lokesh Kumar

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 5/18/2011 [Member] Bronze | Points: 25

Up
0
Down
Hi,
Please check out this link...

http://www.dotnetperls.com/singleton-static

Thanks and Regards
Akiii

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Nithadeepak on: 5/18/2011 [Member] Bronze | Points: 25

Up
0
Down
Refer these links too...
http://www.fredmastro.com/post/C-Singleton-Pattern-vs-Static-Classes.aspx
http://dotnet.dzone.com/news/c-singleton-pattern-vs-static-

Thanks,

Nitha Deepak

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kumar_jay99 on: 5/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi.

in case of singleton class you can create the object of singleton class and pass it to method but
we can not pass the static class to method.

it is easy to change the logic of creating the object with some pooling mechanism but its very difficult
to implement pooling mechanism with static class.

Singleton class does not say any restriction of Inheritence.So we should be able to do this as long as subclass is also
inheritance.There's nothing fundamentally wrong with subclassing a class that is intended to be a singleton.
but We can not inherit Static class to another Static class in C#.

after all singleton is kind of design pattern
Singletons allow you to reuse code and control object state much easier. This improves code-sharing, and can result
in a far cleaner body of code. With less code, your programs will usually have fewer bugs and will be easier to
maintain.

i think above points are enough which demonstrate the some advantage of singleton over static.

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Hemanthlaxmi on: 5/18/2011 [Member] Starter | Points: 25

Up
0
Down
A static class is one that has only static methods, for which a better word would be "functions". The design style embodied in a static class is purely procedural.

Singleton, on the other hand, is a pattern specific to OO design. It is an instance of an object (with all the possibilities inherent in that, such as polymorphism), with a creation procedure that ensures that there is only ever one instance of that particular role over its entire lifetime.

If this helps you .
Please "Mark as Answer"

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Akiii on: 5/18/2011 [Member] Bronze | Points: 25

Up
0
Down
we can dispose the objects of a singleton class but not of static class

Thanks & Regards
Akiii

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Lokesh76 on: 5/18/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks for references.

Thanks
Lokesh Kumar

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sumedhmeshram on: 7/31/2013 [Member] Starter | Points: 25
Posted by: Satyapriyanayak on: 7/31/2013 [Member] [MVP] Silver | Points: 25

Up
0
Down
http://www.dotnetperls.com/singleton-static
http://dotnet.dzone.com/news/c-singleton-pattern-vs-static-
http://www.dofactory.com/topic/1173/difference-singleton-and-static.aspx


If this post helps you mark it as answer
Thanks

Kumar_jay99, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response