Browse Source

整理Singleton代码

tanghai 2 năm trước cách đây
mục cha
commit
02ff13cbda

+ 3 - 3
Unity/Assets/Scripts/Core/World/Module/Config/ConfigComponent.cs

@@ -19,7 +19,7 @@ namespace ET
             public string ConfigName;
         }
 		
-        private readonly ConcurrentDictionary<Type, ISingleton> allConfig = new();
+        private readonly ConcurrentDictionary<Type, ASingleton> allConfig = new();
         
         public void Awake()
         {
@@ -31,7 +31,7 @@ namespace ET
 					EventSystem.Instance.Invoke<GetOneConfigBytes, byte[]>(new GetOneConfigBytes() { ConfigName = configType.Name });
 
 			object category = MongoHelper.Deserialize(configType, oneConfigBytes, 0, oneConfigBytes.Length);
-			ISingleton singleton = category as ISingleton;
+			ASingleton singleton = category as ASingleton;
 			this.allConfig[configType] = singleton;
 			
 			singleton.Register();
@@ -72,7 +72,7 @@ namespace ET
 			
 			lock (this)
 			{
-				ISingleton singleton = category as ISingleton;
+				ASingleton singleton = category as ASingleton;
 				this.allConfig[configType] = singleton;
 				
 				singleton.Register();

+ 6 - 18
Unity/Assets/Scripts/Core/World/Singleton.cs

@@ -1,15 +1,11 @@
-using System;
-using System.ComponentModel;
-
-namespace ET
+namespace ET
 {
-    public interface ISingleton: IDisposable
+    public abstract class ASingleton: DisposeObject
     {
-        void Register();
-        
+        public abstract void Register();
     }
     
-    public abstract class Singleton<T>: ISingleton, ISupportInitialize where T: Singleton<T>, new()
+    public abstract class Singleton<T>: ASingleton where T: Singleton<T>, new()
     {
         private bool isDisposed;
         
@@ -28,7 +24,7 @@ namespace ET
             }
         }
 
-        public virtual void Register()
+        public override void Register()
         {
             Instance = (T)this;
         }
@@ -43,7 +39,7 @@ namespace ET
             
         }
 
-        void IDisposable.Dispose()
+        public override void Dispose()
         {
             if (this.isDisposed)
             {
@@ -54,13 +50,5 @@ namespace ET
             
             this.Destroy();
         }
-
-        public virtual void BeginInit()
-        {
-        }
-
-        public virtual void EndInit()
-        {
-        }
     }
 }

+ 7 - 7
Unity/Assets/Scripts/Core/World/World.cs

@@ -17,7 +17,7 @@ namespace ET
         }
 
         private readonly Stack<Type> stack = new();
-        private readonly Dictionary<Type, ISingleton> singletons = new();
+        private readonly Dictionary<Type, ASingleton> singletons = new();
         
         private World()
         {
@@ -37,7 +37,7 @@ namespace ET
             }
         }
 
-        public T AddSingleton<T>(bool replace = false) where T : class, ISingleton, ISingletonAwake, new()
+        public T AddSingleton<T>(bool replace = false) where T : ASingleton, ISingletonAwake, new()
         {
             T singleton = new();
             singleton.Awake();
@@ -46,7 +46,7 @@ namespace ET
             return singleton;
         }
         
-        public T AddSingleton<T, A>(A a, bool replace = false) where T : class, ISingleton, ISingletonAwake<A>, new()
+        public T AddSingleton<T, A>(A a, bool replace = false) where T : ASingleton, ISingletonAwake<A>, new()
         {
             T singleton = new();
             singleton.Awake(a);
@@ -55,7 +55,7 @@ namespace ET
             return singleton;
         }
         
-        public T AddSingleton<T, A, B>(A a, B b, bool replace = false) where T : class, ISingleton, ISingletonAwake<A, B>, new()
+        public T AddSingleton<T, A, B>(A a, B b, bool replace = false) where T : ASingleton, ISingletonAwake<A, B>, new()
         {
             T singleton = new();
             singleton.Awake(a, b);
@@ -64,7 +64,7 @@ namespace ET
             return singleton;
         }
         
-        public T AddSingleton<T, A, B, C>(A a, B b, C c, bool replace = false) where T : class, ISingleton, ISingletonAwake<A, B, C>, new()
+        public T AddSingleton<T, A, B, C>(A a, B b, C c, bool replace = false) where T : ASingleton, ISingletonAwake<A, B, C>, new()
         {
             T singleton = new();
             singleton.Awake(a, b, c);
@@ -73,7 +73,7 @@ namespace ET
             return singleton;
         }
 
-        public void AddSingleton(ISingleton singleton, bool replace = false)
+        public void AddSingleton(ASingleton singleton, bool replace = false)
         {
             lock (this)
             {
@@ -94,7 +94,7 @@ namespace ET
             {
                 foreach (Type type in this.stack)
                 {
-                    ISingleton singleton = this.singletons[type];
+                    ASingleton singleton = this.singletons[type];
 
                     if (singleton is not ISingletonLoad iSingletonLoad)
                     {

+ 3 - 3
Unity/Assets/Scripts/Editor/Helper/EditorLogHelper.cs

@@ -44,8 +44,8 @@ namespace ET
                 return;
             }
 
-            var log = new Logger();
-            ((ISingleton)log).Register();
+            Logger log = new();
+            log.Register();
             log.ILog = new UnityLogger();
         }
 
@@ -56,7 +56,7 @@ namespace ET
                 return;
             }
 
-            ((ISingleton)Logger.Instance).Dispose();
+            Logger.Instance.Dispose();
         }
     }
 }