2024-10-17 01:59:25 +08:00
|
|
|
|
namespace JNGame.Runtime.Util
|
2024-08-17 14:27:18 +08:00
|
|
|
|
{
|
|
|
|
|
public abstract class SingletonUtil<T> where T : Singleton<T>,new() {
|
|
|
|
|
|
|
|
|
|
public static T Instance{
|
|
|
|
|
get{
|
|
|
|
|
if (Singleton<T>.ins == null)
|
|
|
|
|
{
|
|
|
|
|
Singleton<T>.ins = new T();
|
|
|
|
|
Singleton<T>.ins.Init();
|
|
|
|
|
}
|
|
|
|
|
return Singleton<T>.ins;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void Clean()
|
|
|
|
|
{
|
|
|
|
|
if(Singleton<T>.ins != null)
|
|
|
|
|
Singleton<T>.ins.Clean();
|
|
|
|
|
Singleton<T>.ins = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public class Singleton<T> {
|
|
|
|
|
|
|
|
|
|
public static T ins;
|
|
|
|
|
|
|
|
|
|
public virtual void Init(){}
|
|
|
|
|
public virtual void Clean(){}
|
|
|
|
|
|
|
|
|
|
}
|
2024-10-16 20:41:40 +08:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// 通用单例。
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <typeparam name="T">泛型T。</typeparam>
|
|
|
|
|
public class TSingleton<T> where T : new()
|
|
|
|
|
{
|
|
|
|
|
private static T _instance;
|
|
|
|
|
public static T Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (null == _instance)
|
|
|
|
|
{
|
|
|
|
|
_instance = new T();
|
|
|
|
|
}
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2024-08-17 14:27:18 +08:00
|
|
|
|
}
|