using System; using System.Collections.Generic; namespace XGame.Framework.Map { internal class MapAssetPool { private Dictionary> _viewPool = new Dictionary>(); /// /// 获取ReusableObject (需与Recycle成对) /// /// /// public TEntityView Acquire() where TEntityView : class, IEntityView, new() { Type type = typeof(TEntityView); if (_viewPool.ContainsKey(type)) { var stack = _viewPool[type]; if (stack.Count > 0) return stack.Pop() as TEntityView; } return Activator.CreateInstance(); } ///// ///// 回收 (需与Acquire成对) ///// ///// //public void Recycle(TEntityView obj) where TEntityView : class, IEntityView, new() //{ // Recycle(obj); //} /// /// 回收 (需与Acquire成对) /// /// public void Recycle(IEntityView obj) { if (obj == null) return; var type = obj.GetType(); if (!_viewPool.TryGetValue(type, out var stack)) { stack = new Stack(); _viewPool.Add(type, stack); } obj.Clear(); stack.Push(obj); } public void Clear() { _viewPool.Clear(); } } }