Implementing an inherited singleton class in C #

I found that in C # you can implement the Singleton class as follows:

class Singleton { private static Singleton _instance; public static Singleton Instance { get { return _instance ?? (_instance = new Singleton()); } } protected Singleton() { } } 

What works for instances of type Singleton , ie:

 var a = Singleton.Instance; var b = Singleton.Instance; Console.WriteLine(ReferenceEquals(a, b)); //Prints True. 

But what if I want the derived Singleton classes to also match the Singleton pattern, that is:

 class A:Singleton { ... } A a = A.Instance; 

In this case, the Instance static member gains access to the Singleton class and creates a Singleton instance that is not the target. In addition, there are two main problems with this solution:

  • A derived class may implement its own constructor and lose the Singleton pattern.
  • If there is another instance of Singleton , then the derived class will refer to the less derived instance.

My question is: Is there another way to implement the Singleton class in C #, ensuring that the derived class is also single?

+6
source share
2 answers

Ignoring the usual "Don't use Singleton, look at your design." arguments, you could implement it this way (assuming your derived classes have default constructors):

 public abstract class Singleton<T> where T : class, new() { private static T _instance; public static T GetInstance() { if(_instance == null) _instance = new T(); return _instance; } } 

And get this:

 public class SingletonA : Singleton<SingletonA> { /* .... */ } public class SingletonB : Singleton<SingletonB> { /* .... */ } 

But I'm really not a fan of this singleton approach personally. They have their (rare) uses, but they can turn out to be a pain in the ass - turning into illustrious global variable containers.

Also, be aware of thread safety.

+17
source

My question is: is there another way to implement the Singleton class in C #, ensuring that the derived class is also single?

Well, you might have some kind of check inside the constructor, which:

  • The actual type of this sealed.
  • The actual type of this is a direct subclass of Singleton
  • No instances of this type have been created (while retaining a HashSet<Type> )

However, this seems pointless. What is your base class actually designed to achieve?

The syntax pattern is easily implemented correctly (for example, your example is not thread safe), so why the base class? The base class itself would not be anything (there could potentially be many instances - one per subclass), so what's the use?

It seems to me that β€œI am a single element for the actual type of an object”, in the first place, is not a suitable basis for inheritance, and, frankly, I tried to avoid the syntax pattern as much as possible in any case.

If you really need a base class, this should be because there are some common functions that naturally inherit all subclasses. This is unlikely to be inherently related to whether these subclasses are single.

+3
source

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


All Articles