using System; using System.Collections.Generic; using XGame.Framework.Interfaces; namespace XGame.Framework.Data { internal class DataModule : IDataModule, IDisposable { #region 静态函数 private static DataModule _instance; public static IDataModule Instance { get { if (_instance == null) { _instance = new DataModule(); } return _instance; } } public static void Dispose() { (_instance as IDisposable)?.Dispose(); _instance = null; } #endregion private Dictionary _dataMap = new Dictionary(); TType IDataModule.GetOrAdd() { var type = typeof(TType); if (!_dataMap.TryGetValue(type, out var data)) { data = Activator.CreateInstance(); _dataMap.Add(type, data); (data as IInitialize)?.Initialize(); } return data as TType; } void IDataModule.Remove() { var type = typeof(TType); if(_dataMap.TryGetValue(type, out var data)) { (data as IDisposable)?.Dispose(); _dataMap.Remove(type); } } void IDisposable.Dispose() { foreach (var item in _dataMap.Values) { (item as IDisposable)?.Dispose(); } _dataMap.Clear(); } } }