Reusing code one one C #

I have several classes that do different things but use the same Singleton template from http://www.yoda.arachsys.com/csharp/singleton.html

public sealed class Singleton { static Singleton instance=null; static readonly object padlock = new object(); Singleton() { } public static Singleton Instance { get { lock (padlock) { if (instance==null) { instance = new Singleton(); } return instance; } } } } 

Does anyone have a neat way, can I reuse as much common Singleton code as possible between different classes?

For example, if I have SingletonJob1 and SingletonJob2, I would like to change the code only if I go to one of the other Singleton templates.

Edit: Yes, since people pointed out method 5 from http://www.yoda.arachsys.com/csharp/singleton.html , this is less code. I read to the end of the page! I chose method 2 because Singleton objects are hardware devices, and I want only the pair to be initialized and used in any given program run. Method 5 will initialize them all at once.

+4
source share
5 answers

Is there some reason why you are using this version and not a simple one that just initializes the instance in the declaration?

 public class Singleton { private static Singleton instance = new Singleton(); public static Singleton Instance { get { return instance; } } // Only use this if you really need it - see the page for details static Singleton() {} private Singleton() { // I assume this logic varies } } 

This template is short enough that I donโ€™t think itโ€™s a big problem to include it everywhere.

I would advise you to think about whether you really need a lot of singles; they are generally not suitable for testing, etc.

EDIT: If you really want to practice and you are using .NET 4, you can get it with the 6th template, which is on the singleton page of the new home :

 public sealed class Singleton { private static readonly Lazy<Singleton> lazy = new Lazy<Singleton>(() => new Singleton()); public static Singleton Instance { get { return lazy.Value; } } private Singleton() { } } 
+5
source
 public static class Singleton<T> where T: new() { static T instance=null; static readonly object padlock = new object(); public static T Instance { get { lock (padlock) { if (instance==null) { instance = new T(); } return instance; } } } } 

So you can use Singleton for all classes:

 Singleton<YourClass>.Instance.YourMethod(); 
+3
source

Something like that?

 public sealed class Singleton<T> { static T instance=null; static readonly object padlock = new object(); static Func<T> createInstance; Singleton(Func<T> constructor) { createInstance = constructor; } public static T Instance { get { lock (padlock) { if (instance==null) { instance = createInstance(); } return instance; } } } } 
+2
source

I believe that this is what you need. However, I highly recommend avoiding this singleton (metropolitan S) pattern, as it suppresses all soul testing units and makes heaps of things difficult to control.

 public class Singleton<T> where T : new() { private static T _Instance; private static readonly object _InstanceLock = new object(); public static T Instance { get { if (_Instance == null) { lock (_InstanceLock) { if (_Instance == null) { _Instance = new T(); } } } return _Instance; } } } public class Foo : Singleton<Foo> { public void Something() { } } public class Example { public static void Main() { Foo.Instance.Something(); } } 
+1
source

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


All Articles