简体   繁体   中英

How to manage Singleton instance?

This is a duplicate of: What is a singleton in C#?

I don't think it is duplicate, as here what I am looking for is best strategy for releasing/disposing the Singleto object when not in use.

How to implement a Singleton so that object instance can be released/disposed when all it's references are not in use? And when ever any body wants a Singleton instance it is lazy loaded on demand.

Singletons are not supposed to be disposed of dynamically: once created, they exist till the end of the application's lifetime. Singleton means there is one and only one instance of it.

Even if your Singleton reserves a resource which you want to dynamically release and re-reserve, you shouldn't destroy and rec-create the Singleton instance. That would contradict with the common meaning and usage of the pattern, which can (at best) cause communication problems in your team, or (at worst) subtle bugs in your app.

Instead, you could have the Singleton object internally manage that resource: release it if it hasn't been used for some time, or if its reference count drops to 0.

You should also consider using a Factory instead to access that resource. This gives you much more freedom to control the handling of the resource in question. You can also reuse the created object internally, in effect keeping object count to at most 1.

As some other answers say, Implementing the Singleton Pattern in C# is one of the best resources for Singletons in general.

If you want your singleton to be released when it's not referenced anywhere else, you might take your favorite pattern off the aforementioned site and wrap the instance into a WeakReference , for example something like:

public sealed class Singleton
{
    static private readonly WeakReference _instanceReference =
        new WeakReference(Singleton.LoadInstance());
    static public Singleton Instance
    {
        get { return Singleton.GetInstance(); }
    }

    static private Singleton() { }

    static private Singleton LoadInstance()
    {
        // load from expensive resource;
        return new Singleton();
    }

    static private Singleton GetInstance()
    {
        Singleton result = _instanceReference.Target as Singleton;
        if (result == null)
        {
            // TODO: consider thread safety
            result = LoadInstance();
            _instanceReference.Target = result;
        }

        return result;
    }

    private Singleton()
    {
        //
    }

}

Be aware that consumers most likely would merely call your Singleton.Instance and won't create a reference by themselves, which means your resource would get reloaded quite often. I guess this pattern works best if the Singleton Instance would sometimes be a member of certain classes you pass around.

From Implementing the Singleton Pattern in C# :

public sealed class Singleton
{
    Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return Nested.instance;
        }
    }

    class Nested
    {
        static Nested()
        {
        }
        internal static readonly Singleton instance = new Singleton();
    }
}

As far as the release part it is contradictory to the singleton pattern because if you release the instance the next time someone tries to access it it will get a new instance which violates the singleton pattern.

maybe this will will help you:

http://en.csharp-online.net/Singleton_design_pattern:_Eager/Lazy_Singleton

and this one is about lazy and thread safe singleton:

http://www.yoda.arachsys.com/csharp/singleton.html

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