How to manage an instance of Singleton?

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

I do not think this is a duplicate, since here I am looking for the best strategy for releasing / deleting a Singleto object when not in use.

How to implement Singleton so that an instance of an object can be released / deleted when all links are not in use? And when ever any body wants an instance of Singleton, it lazily loads on demand.

+4
source share
4 answers

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

If you want your singleton to be released when it is not mentioned anywhere, you can remove your favorite template from the above site and wrap the instance in WeakReference , for example:

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() { // } } 

Keep in mind that consumers are likely to just call your Singleton.Instance and not create a link on their own, which means that your resource will often be reloaded. I guess this template works best if the Singleton instance is sometimes a member of certain classes that you pass.

+4
source

Singletones should not be deleted dynamically: after they are created, they exist until the end of the life of the application. Singleton means that there is one and only one instance of it.

Even if your Singleton reserves a resource that you want to dynamically issue and re-reserve, you should not destroy and re-create an instance of Singleton. This will run counter to the general meaning and use of the template, which can (at best) cause communication problems in your team or (at worst) subtle errors in your application.

Instead, you can force a Singleton object to internally manage this resource: release it if it has not been used for some time, or if its reference count drops to 0.

Instead, you should use Factory to access this resource. This gives you much more freedom to control the processing of the resource in question. You can also reuse the created object internally, effectively keeping the number of objects no more than 1.

+7
source

From Implementing the Singleton Template 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 for the release part, this contradicts the singleton pattern, because if you release the instance, then the next time someone tries to access it, they will get a new instance that violates the singleton pattern.

+3
source

maybe this will help you:

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

and this question relates to a lazy and thread-safe singleton:

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

+2
source

Source: https://habr.com/ru/post/1301547/


All Articles