Generic Singleton

2019-03-27 09:30发布

I have a question, is this the correct approach to make a Generic Singleton?

 public class Singleton<T> where T : class, new()
    {
        private static T instance = null;

        private Singleton() { }

        public static T Instancia
        {
            get 
            {
                if (instance == null)
                    instance = new T();
                return instance;
            }
        }
    }

EDIT:

Checking some PDFs I found a generic Singleton made this other way, is this other correct?

public class Singleton<T> where T : class, new()
{
    Singleton() { }

    class SingletonCreator
    {
        static SingletonCreator() { }
        // Private object instantiated with private constructor
        internal static readonly T instance = new T();
    }

    public static T UniqueInstance
    {
        get { return SingletonCreator.instance; }
    }
}

8条回答
Viruses.
2楼-- · 2019-03-27 10:24

It is possible without reflection.

We just need a generic class for the singleton pattern which takes two parameter - the implementation of the concrete singleton class and the interface for the concrete singleton. The generic singleton class implements the singleton pattern and all the stuff you need - for example logging, locks or what else.

using System;
using System.Diagnostics;

namespace Singleton
{
    class Program
    {
        static void Main(string[] args)
        {
            Something.Instance.SayHello();
        }
    }

    /// <summary>
    /// Generic singleton pattern implementation
    /// </summary>
    public class SingletonImplementation<Implementation, ImplementationInterface>
           where Implementation : class, ImplementationInterface, new()
    {
        private SingletonImplementation() { }

        private static Implementation instance = null;
        public static ImplementationInterface Instance
        {
            get
            {
                // here you can add your singleton stuff, which you don't like to write all the time

                if ( instance == null )
                {
                    instance = new Implementation();
                }

                return instance;
            }
        }
    }

    /// <summary>
    /// Interface for the concrete singleton
    /// </summary>
    public interface ISomething
    {
        void SayHello();
    }

    /// <summary>
    /// Singleton "held" or "wrapper" which provides the instance of the concrete singleton
    /// </summary>
    public static class Something
    {
        // No need to define the ctor private, coz you can't do anything wrong or useful with an instance of Something
        // private Implementation();

        /// <summary>
        /// Like common: the static instance to access the concrete singleton
        /// </summary>
        public static ISomething Instance => SingletonImplementation<ImplementationOfSomething, ISomething>.Instance;

        /// <summary>
        /// Concrete singleton implementation
        /// </summary>
        private class ImplementationOfSomething : ISomething
        {
            // No need to define the ctor private, coz the class is private.
            // private Implementation();

            public void SayHello()
            {
                Debug.WriteLine("Hello world.");
            }
        }
    }
}
查看更多
倾城 Initia
3楼-- · 2019-03-27 10:25

For a generic piece of code that will be reused, you should consider thread safety at the point where you create the singleton instance.

As it is, (instance == null) could evaluate to true on separate threads.

查看更多
登录 后发表回答