using System;

namespace XGame.Framework
{
    public class TSingleton<TType> where TType : TSingleton<TType>, new()
    {
        private static TType _instance;

        public static void Dispose()
        {
            _instance?.OnDispose();
            _instance = null;
        }

        public static TType Instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = Activator.CreateInstance<TType>();
                    _instance.OnInit();
                }
                return _instance;
            }
        }


        protected virtual void OnInit() { }
        protected virtual void OnDispose() { }

    }
}

