locked
What is difference between singleton class and static class in terms of usage ? RRS feed

  • Question

  • User264732274 posted

    i am familiar with singleton class and static class too but do not decide when to use singleton class and when static class. so anyone would guide me what is the difference between singleton class and static class and when we should use singleton class and when should go for static class with example code and scenario. thanks

    Wednesday, October 28, 2015 10:15 AM

Answers

  • User603616845 posted

    Hi,

    There are lots of difference between singleton and static class.

    1. Singleton object stores in Heap but, static object stores in stack
    2. We can clone the object of Singleton but, we can not clone the static class object
    3. we can implement interface with Singleton class but not with Static class.

    First you need to know the difference between static and singleton then you can move ahead. You can also refer this article.

    Hope this will help you.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2015 10:45 AM
  • User-434868552 posted

    @sudip_inn

    a singleton is NOT a class, a singleton is a design pattern

    https://en.wikipedia.org/wiki/Singleton_pattern 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2015 12:08 PM
  • User603616845 posted
    Hi,

    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.

    http://net-informations.com/faq/netfaq/singlestatic.htm

    I hope this will make your doubt clear.
    Thanks
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2015 12:21 AM
  • User603616845 posted

    Real World Example of a Singleton Class

    Thread pools, SQL Connection Pools, Registry objects, Objects handling user preferences, Caches, Factory classes, Builder classes and Statistics utilities like a hit counter, log4net, when you call its logger, it uses a singleton class to return it.

    Difference between Singleton and Static Class

    Another question that usually comes up when it comes to using a Singleton is “Why not just use a static class?”. Static classes still have many uses and lots of times, people get confused and will use a Singleton as much as possible. One easy rule of thumb you can follow is if it doesn’t need to maintain state, you can use a Static class, otherwise you should use a Singleton.

    So here is a quick list of uses for static classes:
    Math.pow(double a, double b);
    Interger.parseInt(String s);
    Interger.toString(int i);

    As you can see, the state of these methods don’t matter. You just want to use them to perform a simple task for you. But if you coding your application and you are using a central object where state does matter(such as the ModelLocator example), then its best to use a Singleton.

    The next reason you may want to use a Singleton is if it is a particularly “heavy” object. If your object is large and takes up a reasonable amount of memory, you probably only one of those objects floating around. This is the case for things like a if you have a factory method that is particularly robust, you want to make sure that its not going to be instantiated multiple times. A Singleton class will help prevent such the case ever happening.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2015 6:10 AM
  • User-434868552 posted

    @sudip_inn  you are welcome.

    would discuss this topic with scenario like which things we should develop following singleton pattern and when we should go for static class. looking for details discussion. thanks

    you must not confuse the singleton design pattern with the concept of a static class.

    i recommend you buy Judith Bishop's book "C# 3.0 Design Patterns", O'Reilly, ISBN 0-596-52773-X, January 2008.

    Companion Website at http://patterns.cs.up.ac.za 

    http://research.microsoft.com/en-us/people/jbishop/ "Judith Bishop is Director of Computer Science in Microsoft Research Connections"

    "Her research expertise is in programming languages and distributed systems,
     with a strong practical bias and interest in compilers and design patterns"

    you may also be able to get her book from your local library.

    do visit her companion website and study the code examples.

    for the Singleton Pattern, study pages 115 to 121 of "C# 3.0 Design Patterns".  Judith Bishop discusses its rôle, gives an illustration, discusses the design and implementation, provides an example and writes about its use.  She also provides exercises.

    sudip_inn, search Google for       when should i use a static c# class

    study the links you will find like:

    https://msdn.microsoft.com/en-CA/library/79b3xss3.aspx "Static Classes and Static Class Members (C# Programming Guide)"

    http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp 

    et cetera

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2015 12:27 PM

All replies

  • User603616845 posted

    Hi,

    There are lots of difference between singleton and static class.

    1. Singleton object stores in Heap but, static object stores in stack
    2. We can clone the object of Singleton but, we can not clone the static class object
    3. we can implement interface with Singleton class but not with Static class.

    First you need to know the difference between static and singleton then you can move ahead. You can also refer this article.

    Hope this will help you.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2015 10:45 AM
  • User-434868552 posted

    @sudip_inn

    a singleton is NOT a class, a singleton is a design pattern

    https://en.wikipedia.org/wiki/Singleton_pattern 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, October 28, 2015 12:08 PM
  • User264732274 posted

    @Gerry thanks

    would discuss this topic with scenario like which things we should develop following singleton pattern and when we should go for static class. looking for details discussion. thanks

    Wednesday, October 28, 2015 3:21 PM
  • User264732274 posted

    @Mukesh please see what i wrote to @Gerry and guide me accordingly. thanks

    Wednesday, October 28, 2015 3:21 PM
  • User603616845 posted
    Hi,

    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.

    http://net-informations.com/faq/netfaq/singlestatic.htm

    I hope this will make your doubt clear.
    Thanks
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2015 12:21 AM
  • User264732274 posted

    @Mukesh i am not looking for difference between singleton class and static class rather i like to know what part of code in project should be design following singleton class and which one should be use static class.

    you said Static= Static an not have any object pointer & the scope of static is at App Domain level.

    why u said static not have any object ? when program run then memory also allocated for static class. you can say we do not create any object for static class but it does not mean no memory is allocated for static class.

    looking for your remarks. thanks

    Thursday, October 29, 2015 4:32 AM
  • User603616845 posted

    Real World Example of a Singleton Class

    Thread pools, SQL Connection Pools, Registry objects, Objects handling user preferences, Caches, Factory classes, Builder classes and Statistics utilities like a hit counter, log4net, when you call its logger, it uses a singleton class to return it.

    Difference between Singleton and Static Class

    Another question that usually comes up when it comes to using a Singleton is “Why not just use a static class?”. Static classes still have many uses and lots of times, people get confused and will use a Singleton as much as possible. One easy rule of thumb you can follow is if it doesn’t need to maintain state, you can use a Static class, otherwise you should use a Singleton.

    So here is a quick list of uses for static classes:
    Math.pow(double a, double b);
    Interger.parseInt(String s);
    Interger.toString(int i);

    As you can see, the state of these methods don’t matter. You just want to use them to perform a simple task for you. But if you coding your application and you are using a central object where state does matter(such as the ModelLocator example), then its best to use a Singleton.

    The next reason you may want to use a Singleton is if it is a particularly “heavy” object. If your object is large and takes up a reasonable amount of memory, you probably only one of those objects floating around. This is the case for things like a if you have a factory method that is particularly robust, you want to make sure that its not going to be instantiated multiple times. A Singleton class will help prevent such the case ever happening.

    thanks

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2015 6:10 AM
  • User-434868552 posted

    @sudip_inn  you are welcome.

    would discuss this topic with scenario like which things we should develop following singleton pattern and when we should go for static class. looking for details discussion. thanks

    you must not confuse the singleton design pattern with the concept of a static class.

    i recommend you buy Judith Bishop's book "C# 3.0 Design Patterns", O'Reilly, ISBN 0-596-52773-X, January 2008.

    Companion Website at http://patterns.cs.up.ac.za 

    http://research.microsoft.com/en-us/people/jbishop/ "Judith Bishop is Director of Computer Science in Microsoft Research Connections"

    "Her research expertise is in programming languages and distributed systems,
     with a strong practical bias and interest in compilers and design patterns"

    you may also be able to get her book from your local library.

    do visit her companion website and study the code examples.

    for the Singleton Pattern, study pages 115 to 121 of "C# 3.0 Design Patterns".  Judith Bishop discusses its rôle, gives an illustration, discusses the design and implementation, provides an example and writes about its use.  She also provides exercises.

    sudip_inn, search Google for       when should i use a static c# class

    study the links you will find like:

    https://msdn.microsoft.com/en-CA/library/79b3xss3.aspx "Static Classes and Static Class Members (C# Programming Guide)"

    http://stackoverflow.com/questions/241339/when-to-use-static-classes-in-c-sharp 

    et cetera

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, October 29, 2015 12:27 PM