C#泛型单例

1.不支持非公共的无参构造函数的

using System;
using UnityEngine;
public abstract class Singleton<T> where T : class, new()
{
	private static T m_instance;
	public static T instance
	{
		get
        {
            if (Singleton<T>.m_instance == null)
		    {
			    Singleton<T>.m_instance = Activator.CreateInstance<T>();
			    if (Singleton<T>.m_instance != null)
			    {
                    (Singleton<T>.m_instance as Singleton<T>).Init();
			    }
		    }

            return Singleton<T>.m_instance;
        }
	}

	public static void Release()
	{
		if (Singleton<T>.m_instance != null)
		{
			Singleton<T>.m_instance = (T)((object)null);
		}
	}

    public virtual void Init()
    {

    }

    public abstract void Dispose();

}

2.支持非公共的无参构造函数的

public class BaseInstance<T> where T : class//new(),new不支持非公共的无参构造函数 
    {
        /*
         * 单线程测试通过!
         * 多线程测试通过!
         * 根据需要在调用的时候才实例化单例类!
          */
        private static T _instance;
        private static readonly object SyncObject = new object();
        public static T Instance
        {
            get
            {
                if (_instance == null)//没有第一重 singleton == null 的话,每一次有线程进入 GetInstance()时,均会执行锁定操作来实现线程同步,
                //非常耗费性能 增加第一重singleton ==null 成立时的情况下执行一次锁定以实现线程同步
                {
                    lock (SyncObject)
                    {
                        if (_instance == null)//Double-Check Locking 双重检查锁定
                        {
                            //_instance = new T();
                            //需要非公共的无参构造函数,不能使用new T() ,new不支持非公共的无参构造函数 
                            _instance = (T)Activator.CreateInstance(typeof(T), true); //第二个参数防止异常:“没有为该对象定义无参数的构造函数。”
                        }
                    }
                }
                return _instance;
            }
        }
        public static void SetInstance(T value)
        {
            _instance = value;
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章