none
What is singleton and what its usage RRS feed

  • Question

  • we know singleton means one instance creation but i want to understand what is real usage of singleton?

    what is advantage of singleton design pattern and in what kind of scenario it make sense?

    without singleton we can do our job by static class. give me some sample scenario where people would always go for singleton design pattern.

    please discuss this topic in details with few sample code and scenario. thanks

    Sunday, March 1, 2020 6:50 PM

Answers

  • Hi Tridip,

    Thank you for posting here.

    The singleton design pattern is one of the twenty-three well-known "Gang of Four" design patterns. Its advantages are mainly reflected in the following points.

    1. Instance control: Singleton prevents other objects from instantiating their own copies of the Singleton object, ensuring that all objects access the single instance.

    2. Flexibility: Since the class controls the instantiation process, the class has the flexibility to change the instantiation process.

    3. Easy to implement: Easy to create and we could use it anywhere for the lifetime of the app The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton. However, you can still change your mind and manage any number of instances. Having a very versatile design pattern that can work well for many different applications and processes.

    In a sense, it has similarities to static classes, for a discussion of this, check out the link below.

    Difference between static class and singleton pattern?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 2, 2020 5:43 AM

All replies

  • Hello,

    First read the following page then check out several examples in my GitHub repository (my examples use the sixth version of singleton from the first link but dependent on needs is not always the choice to use) and Microsoft TechNet article.

    Now before using Singleton make sure you first discounted other methods e.g. using a public class with static methods for example.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Sunday, March 1, 2020 7:53 PM
    Moderator
  • Basically a Singleton is a design pattern that can be used when is expensive for you instantiate new elements in your project.

    Instead instantiate and dispose a element many times, you can instantiate once and keep it in memory until you want, this time time, if other component need you component it can just use it.

    It is important to consider if you singleton object spend too much resources, in this case Singleton could not be a good pattern to use.

    Sunday, March 1, 2020 9:23 PM
  • Hi Tridip,

    Thank you for posting here.

    The singleton design pattern is one of the twenty-three well-known "Gang of Four" design patterns. Its advantages are mainly reflected in the following points.

    1. Instance control: Singleton prevents other objects from instantiating their own copies of the Singleton object, ensuring that all objects access the single instance.

    2. Flexibility: Since the class controls the instantiation process, the class has the flexibility to change the instantiation process.

    3. Easy to implement: Easy to create and we could use it anywhere for the lifetime of the app The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton. However, you can still change your mind and manage any number of instances. Having a very versatile design pattern that can work well for many different applications and processes.

    In a sense, it has similarities to static classes, for a discussion of this, check out the link below.

    Difference between static class and singleton pattern?

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, March 2, 2020 5:43 AM
  • Hi Tridip,

    Has your problem been solved?

    If so, please click "Mark as answer" to the appropriate answer, so that it will help other members to find the solution quickly if they face a similar issue.

    Best Regards,

    Timon


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, March 10, 2020 6:09 AM