简体   繁体   中英

what is the advantage of Singleton Design Pattern

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;   
        }   
    }   
}

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.

Other answers are good, as well. But they are providing examples of behavioural characteristics of the pattern. But, Singleton is more about creation . Thus one of the most important benefit of the pattern is that it is resource friendly. You are not wasting memory for a new object when you actually do not need a new one.

This causes another benefit, which is the instantiation overhead is avoided.

Benefits of Singleton Pattern:

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

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

The advantage of Singleton over global variables is that you are absolutely sure of the number of instances when you use Singleton, and, you can change your mind and manage any number of instances.

Real time usages/benefits of Singleton Design Pattern.

  1. While using multi-threading, to manage the multi-thread Pool.
  2. to manage the "service host repositories" in SOA (service oriented architecture).
  3. for Logging Framework implementation
  4. in automation Testing/Unit Testing project ie Coded UI projects.
  5. While implementing the Caching in a big application.
  6. for configuration settings to make proper control over the application.

Generally singleton is considered an anti-pattern in OOP because it means a class is asserting that with respect to the entire program - which in OOP it should have no knowledge of - it knows it's going to be the only one. That being said, singleton is the proper way to implement a constant in my experience. In general if something I was about to hard-code into a program (say a database username) then it can be moved to a Config file or a singleton.

One of few areas Java beats C# (in my opinion...) is its support for enums. Java offers truly OO constants via enums, and so this is how I will always implement a singleton in Java. C# has no ready-equivalent.

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.

It can improve the way that memory is handled in the JVM and with memory being used properly, better performance will be reached. You are not creating multiple objects but trying to create only one, that way, there is less work for the Garbage collector and less memory occupation in the JVM heap.

让我们假设有一台打印机,所有人都必须访问该打印机,然后在创建对象时,您应该只允许一个人访问打印,因为它不允许另一个人同时打印,这就是为什么在现实生活中的这种情况下我们需要单身吨类,我们可以更清晰地一一管理任务......

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM