/*
 *Copyright(C) 2023 by Chief All rights reserved.
 *Unity版本：2023.2.5f1c1 
 *作者:Chief  
 *创建日期: 2024-01-19 
 *模块说明：通用单例模块
 *版本: 1.0
*/

namespace Aster
{
    /// <summary>
    /// 单例；
    /// 线程安全
    /// </summary>
    public class Singleton<T> where T : Singleton<T>, new()
    {

        private static T _instance;
        private static readonly object syslock = new object();

        public static T getInstance()
        {
            if (_instance == null)
            {
                lock (syslock)
                {
                    if (_instance == null)
                    {
                        _instance = new T();
                        _instance.OnCreate();
                    }
                }
            }
            return _instance;
        }

        public void Destroy()
        {
            _instance?.OnRelease();
            _instance = null;
        }

        public static T Instance { get { return getInstance(); } }

        public static T Get => Instance;

        protected virtual void OnCreate() { }

        protected virtual void OnRelease() { }

    }
}