using System; using System.Collections; using System.Collections.Generic; namespace Stary.Evo { public static class Pool { public readonly static List AllPool = new List(); public static void ReleaseAll() { foreach (var pool in AllPool) { pool.Dispose(); } AllPool.Clear(); } } public interface IObject { void OnRelease(); } public interface PoolBase { void Dispose(); } public class ObjectPool : PoolBase where T : new() { private static ObjectPool Instance; private Stack _pool; private ObjectPool() { } private static void Init() { if (Instance == null) { Instance = new ObjectPool(); Instance._pool = new Stack(); Pool.AllPool.Add(Instance); } } public static T Get() { Init(); if (Instance._pool.Count > 0) { return Instance._pool.Pop(); } else { return new T(); } } public static void Release(T obj) { if (obj == null || Instance == null) return; if (obj is IObject interfac) { interfac.OnRelease(); } Instance._pool.Push(obj); } public void Dispose() { if (Instance != null) { if (Instance._pool != null) { Instance._pool.Clear(); Instance._pool = null; } Instance = null; } } } public class ListPool : PoolBase { private static ListPool Instance; private Stack> _pool; private ListPool() { } private static void Init() { if (Instance == null) { Instance = new ListPool(); Instance._pool = new Stack>(); Pool.AllPool.Add(Instance); } } public static List Get() { Init(); if (Instance._pool.Count > 0) { return Instance._pool.Pop(); } else { return new List(); } } public static void Release(List list) { if (list == null || Instance == null) return; list.Clear(); Instance._pool.Push(list); } public void Dispose() { if (Instance != null) { if (Instance._pool != null) { Instance._pool.Clear(); Instance._pool = null; } Instance = null; } } } public class DictionaryPool : PoolBase { private static DictionaryPool Instance; private Stack> _pool; private DictionaryPool() { } private static void Init() { if (Instance == null) { Instance = new DictionaryPool(); Instance._pool = new Stack>(); Pool.AllPool.Add(Instance); } } public static Dictionary Get() { Init(); if (Instance._pool.Count > 0) { return Instance._pool.Pop(); } else { return new Dictionary(); } } public static void Release(Dictionary dict) { if (dict == null || Instance == null) return; dict.Clear(); Instance._pool.Push(dict); } public void Dispose() { if (Instance != null) { if (Instance._pool != null) { Instance._pool.Clear(); Instance._pool = null; } Instance = null; } } } }