S
S
stupid_Dotnetchik2018-02-28 22:23:20
C++ / C#
stupid_Dotnetchik, 2018-02-28 22:23:20

Is the implementation of singleton inheritance correct?

Ku!
A small desktop application, there are a couple of dozen classes, the objects of which you don’t want to produce.
The first solution that comes to mind is to make them static, with all the ensuing consequences: all class fields are static, all methods are also static, which seems not very correct in an OOP language.
The second idea is to make everything singletons:

public class YellowClass
  {
    private static YellowClass _instance;
    private YellowClass() { }
    public static YellowClass GetInstance()
    {
      return _instance ?? (_instance = new YellowClass());
    }
  }

However, copy-pasting 6 lines of code into each class (in the examples more difficult with a thread safe and more) seems very dull.
I found several examples of 'inherited singletons', I thought that happiness is close. One of them: pr
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;

namespace DemoApplication
{
  static class Program
  {
    static void Main(string[] args)
    {
      var yellowClass1 = new YellowClass();
      var yellowClass2 = YellowClass.Instance;

      Debug.Print(yellowClass1.Equals(yellowClass2) ? "All good!" : "WTF???");
    }
  }

  public class Singleton<T>
  {
    [SuppressMessage("ReSharper", "InconsistentNaming")]
    private static readonly Lazy<T> _instance = new Lazy<T>(CreateInstance);

    private static T CreateInstance()
    {
      return (T)Activator.CreateInstance(typeof(T), true);
    }

    public static T Instance
    {
      get { return _instance.Value; }
    }
  }

  public class YellowClass : Singleton<YellowClass>
  {
  }
}

It seemed to me that the singleton implies the existence of only 1 instance of the class in the virtual machine, and here I create 1 more instance with the constructor. Of course, you can add private constructors for all descendant classes, but I (or a colleague) can simply forget to do this in six months when adding a couple more classes of this category, which can lead to insidious bugs (there have already been cases).
I googled bad examples of singleton inheritance? Maybe you can somehow do without a private constructor in each derived class?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Denis Zagaevsky, 2018-02-28
@zagayevskiy

In my opinion, garbage and nonsense. Discover dependency injection.

G
Griboks, 2018-02-28
@Griboks

Well, in order not to impose additional restrictions on T (in this case, the presence of a non-parameterized constructor), you can write this: (T)Activator.CreateInstance(typeof(T), true);

So maybe it was worth reading the description of this example first?

E
eRKa, 2018-03-01
@kttotto

I use this design, time-tested

public class Singleton<T> where T : class
{
  private static readonly Lazy<T> _instance = new Lazy<T>(() => (T)typeof(T).GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public, null, new Type[0], null).Invoke(null), true);

  public static T Instance => _instance.Value;
}

public sealed class CustomSingleton : Singleton<CustomSingleton>
{
}

var single = CustomSingleton.Instance

P
Pavlo Ponomarenko, 2018-04-11
@TheShock

The first solution that comes to mind is to make them static, with all the ensuing consequences: all class fields are static, all methods are also static, which seems not very correct in an OOP language.

If you want to write procedurally - write procedurally) The fact that you make everything singletons will not make your code OOP-shny. He will only pretend to be OOP, but not be. But maybe you don't need it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question