通過泛型寫一個通用的單例

  • 寫一個通用的單例,並且進行數據初始化以及單例的釋放

    public class TSingleton<T> where T : new()
    {
        static T m_instance;
        public static  T Instance()
        {
            if (m_instance == null)
            {
                m_instance = new T();
                (m_instance as TSingleton<T>).init();
            }
    
            return m_instance;
        }
    
        /// <summary>
        /// 初次調用可以進行數據的初始化
        /// </summary>
        virtual protected void init()
        {
    
        }
    
        /// <summary>
        /// 單例釋放
        /// </summary>
        public void Release()
        {
            m_instance = default(T);
        }
    }
發佈了37 篇原創文章 · 獲贊 20 · 訪問量 6萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章