locked
Static class vs singleton class RRS feed

  • Question

  • hi,

    the main objective of both static class and a singleton class is that having a shared entity. but what is the basic difference between them?> which to use where?

    thanks in advance

    Swapnil

    Saturday, May 1, 2010 12:55 PM

Answers

  • A "singleton" can be viewed as a shared instance of a class.  This does not necessarily preclude there being other instances.*  For example, System.Text.Encoding.Unicode is a singleton, but you can create instances of Unicode that are configured differently (e.g., you can change how it deals with errors).  Even if you are only going to have one instance of the class right now, you might need the ability to have multiple instances in the future.  Going with the singleton gives you this flexibility for the future.  (Note:  You can make the constructor private or internal--not public--to tightly control the creation of instances to what you support.)

    Another advantage of a singleton is that you can implement interfaces or derive from other classes.  This is essential for some scenarios.

    As you can see, you can usually reduce your program so that the only public static properties are the properties that return singleton instances.  I think this is a very robust way of structuring the program.

    A shared public method is okay for an operation such as an mathematical function where there is only one definition. 

    *A matter of definition.  Even if you take the definition as meaning a class that only has one instance, you still should be familiar with the various possibilities described here.

    • Proposed as answer by insigniya Monday, May 3, 2010 9:54 AM
    • Marked as answer by SamAgain Friday, May 7, 2010 9:16 AM
    Saturday, May 1, 2010 7:05 PM
  • Hi Swapnil,

        Basically speaking, they are meant for different purposes.

        A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. It has the following characteristics:

        - Contains only static members.

        - Cannot be instantiated.

        - Is sealed.

        - Cannot contain Instance Constructors.

        The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

        A singleton class is used when you need a class that has only one instance, and you need to provide a global point of access to the instance. A singleton class has a private constructor. A private constructor prevents the class from being instantiated.

        A singleton class can have instance members, while a static class cannot.

        When designing a singleton class with .NET, you want to be sure that your solution is efficient and that it takes advantage of the Microsoft .NET CLR features. You may also want to make sure that your solution is thread safe. There're several ways to enforce thread-safety, such as static initialization or double-check locking. The System.Text.Encoding.Unicode, as mentioned by BinaryCoder, take the static initialization approach. You can verify that with the .NET Reflector.

        Following are some additional references:

        0. Static Classes and Static Class Members (C# Programming Guide)

        1. Implementing Singleton in C#

        2. Implementing the Singleton Pattern in C#

        3. Singleton Class Private Constructor, C# Singleton Class, and More

     

     


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Proposed as answer by SamAgain Friday, May 7, 2010 5:57 AM
    • Marked as answer by SamAgain Friday, May 7, 2010 9:16 AM
    Friday, May 7, 2010 5:57 AM

All replies

  • A "singleton" can be viewed as a shared instance of a class.  This does not necessarily preclude there being other instances.*  For example, System.Text.Encoding.Unicode is a singleton, but you can create instances of Unicode that are configured differently (e.g., you can change how it deals with errors).  Even if you are only going to have one instance of the class right now, you might need the ability to have multiple instances in the future.  Going with the singleton gives you this flexibility for the future.  (Note:  You can make the constructor private or internal--not public--to tightly control the creation of instances to what you support.)

    Another advantage of a singleton is that you can implement interfaces or derive from other classes.  This is essential for some scenarios.

    As you can see, you can usually reduce your program so that the only public static properties are the properties that return singleton instances.  I think this is a very robust way of structuring the program.

    A shared public method is okay for an operation such as an mathematical function where there is only one definition. 

    *A matter of definition.  Even if you take the definition as meaning a class that only has one instance, you still should be familiar with the various possibilities described here.

    • Proposed as answer by insigniya Monday, May 3, 2010 9:54 AM
    • Marked as answer by SamAgain Friday, May 7, 2010 9:16 AM
    Saturday, May 1, 2010 7:05 PM
  • Hi Swapnil,

        Basically speaking, they are meant for different purposes.

        A static class can be used as a convenient container for sets of methods that just operate on input parameters and do not have to get or set any internal instance fields. It has the following characteristics:

        - Contains only static members.

        - Cannot be instantiated.

        - Is sealed.

        - Cannot contain Instance Constructors.

        The advantage of using a static class is that the compiler can check to make sure that no instance members are accidentally added. The compiler will guarantee that instances of this class cannot be created.

        A singleton class is used when you need a class that has only one instance, and you need to provide a global point of access to the instance. A singleton class has a private constructor. A private constructor prevents the class from being instantiated.

        A singleton class can have instance members, while a static class cannot.

        When designing a singleton class with .NET, you want to be sure that your solution is efficient and that it takes advantage of the Microsoft .NET CLR features. You may also want to make sure that your solution is thread safe. There're several ways to enforce thread-safety, such as static initialization or double-check locking. The System.Text.Encoding.Unicode, as mentioned by BinaryCoder, take the static initialization approach. You can verify that with the .NET Reflector.

        Following are some additional references:

        0. Static Classes and Static Class Members (C# Programming Guide)

        1. Implementing Singleton in C#

        2. Implementing the Singleton Pattern in C#

        3. Singleton Class Private Constructor, C# Singleton Class, and More

     

     


    Please mark the right answer at right time.
    Thanks,
    Sam
    • Proposed as answer by SamAgain Friday, May 7, 2010 5:57 AM
    • Marked as answer by SamAgain Friday, May 7, 2010 9:16 AM
    Friday, May 7, 2010 5:57 AM