Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the advantage of Singleton Design Pattern

Tags:

c#

oop

every one know how to write code for Singleton Design Pattern.say for example

public class Singleton  
{  
    // Private static object can access only inside the Emp class.  
    private static Singleton instance;  

    // Private empty constructor to restrict end use to deny creating the object.  
    private Singleton()  
    {  
    }  

    // A public property to access outside of the class to create an object.  
    public static Singleton Instance  
    {  
        get  
        {  
            if (instance == null)  
            {  
                instance = new Singleton();  
            }  
            return instance;  
        }  
    }  
}  

it is very clear that when we create a instance of any class many time the memory is allocated for each instance but in case of Singleton design pattern a single instance give the service for all calls.

1) i am bit confuse and really do nor realize that what are the reasons...that when one should go for Singleton Design Pattern. only for saving some memory or any other benefit out there.

2) suppose any single program can have many classes then which classes should follow the Singleton Design Pattern? what is the advantage of Singleton Design Pattern?

3 in real life apps when should one make any classes following Singleton Design Pattern? thanks

Here is thread safe singleton

public sealed class MultiThreadSingleton   
{   
    private static volatile MultiThreadSingleton instance;   
    private static object syncRoot = new Object();   

    private MultiThreadSingleton()   
    {   
    }   

    public static MultiThreadSingleton Instance   
    {   
        get   
        {   
            if (instance == null)   
            {   
                lock (syncRoot)   
                {   
                    if (instance == null)   
                    {   
                        instance = new MultiThreadSingleton();   
                    }   
                }   
            } 

        return instance;   
        }   
    }   
}
like image 288
Thomas Avatar asked Oct 15 '12 18:10

Thomas


People also ask

What is the advantage of using Singleton design pattern?

It solves the problem of creating only one object of one class and that object can be used where ever required. Singleton is a creational pattern. This pattern ensures that there is only one instance of a class and provides a global access point for this object.

What is the advantage and disadvantages of using a singleton pattern?

Singletons hinder unit testing: A Singleton might cause issues for writing testable code if the object and the methods associated with it are so tightly coupled that it becomes impossible to test without writing a fully-functional class dedicated to the Singleton.

What is the advantage of Singleton design pattern in Swift?

The main advantage of the singleton pattern is its ease of access. Wherever you need the shared instance, you can access it without modifying your code further. It's also possible to enforce a single instance if you make the Facade class's initializer private.

What is the disadvantage of Singleton?

The most important drawback of the singleton pattern is sacrificing transparency for convenience. Consider the earlier example. Over time, you lose track of the objects that access the user object and, more importantly, the objects that modify its properties.


2 Answers

To assure only one and same instance of object every time.

Take a scenario, say for a Company application, there is only one CEO. If you want to create or access CEO object, you should return the same CEO object every time.

One more, after logging into an application, current user must return same object every time.

like image 134
sgud Avatar answered Oct 03 '22 04:10

sgud


One useful place to use a singleton is if it is accessing some resource that you only want to have a single access point for. For example, I've used it when writing some code to talk to a device. I only want one piece of code talking to the device so I use a singleton. Any attempt to create another instance of the object that talks to the device will just give you the same object back so I never have to worry about two instances maintaining out-of-sync data about the device or getting messages to and from the device mixed up or out-of-order.

But, of course, you are under no obligation to use them. They are just a tool that is sometimes useful.

like image 23
Matt Burland Avatar answered Oct 03 '22 04:10

Matt Burland