Files
test/Assets/PerfectWorld/Scripts/Common/Singleton.cs
T
2025-11-07 18:01:47 +07:00

22 lines
648 B
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BrewMonster.Assets.PerfectWorld.Scripts.Common
{
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!");
}
}
}