Files
test/Assets/PerfectWorld/Scripts/Common/Singleton.cs
T
2026-02-02 19:21:45 +07:00

27 lines
691 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!");
}
protected virtual void Initialize()
{
}
}
}