Files
test/Assets/PerfectWorld/Scripts/Common/Singleton.cs
T
2025-12-02 16:46:47 +07:00

22 lines
613 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BrewMonster
{
public abstract class Singleton<T> where T : class, new()
{
private static readonly Lazy<T> _instance = new Lazy<T>(() => new T());
public static T Instance => _instance.Value;
// Protected constructor prevents external instantiation
protected Singleton()
{
if (_instance.IsValueCreated)
throw new InvalidOperationException($"Singleton<{typeof(T).Name}> already created!");
}
}
}