diff --git a/GFramework.Core.Abstractions/lifecycle/IDisposable.cs b/GFramework.Core.Abstractions/lifecycle/IDisposable.cs new file mode 100644 index 0000000..7cfc151 --- /dev/null +++ b/GFramework.Core.Abstractions/lifecycle/IDisposable.cs @@ -0,0 +1,12 @@ +namespace GFramework.Core.Abstractions.lifecycle; + +/// +/// 可销毁接口,为需要资源清理的组件提供标准销毁能力 +/// +public interface IDisposable +{ + /// + /// 销毁组件并释放资源 + /// + void Destroy(); +} \ No newline at end of file diff --git a/GFramework.Core.Abstractions/lifecycle/IInitializable.cs b/GFramework.Core.Abstractions/lifecycle/IInitializable.cs new file mode 100644 index 0000000..6e389e9 --- /dev/null +++ b/GFramework.Core.Abstractions/lifecycle/IInitializable.cs @@ -0,0 +1,12 @@ +namespace GFramework.Core.Abstractions.lifecycle; + +/// +/// 可初始化接口,为需要初始化的组件提供标准初始化能力 +/// +public interface IInitializable +{ + /// + /// 初始化组件 + /// + void Init(); +} \ No newline at end of file diff --git a/GFramework.Core.Abstractions/lifecycle/ILifecycle.cs b/GFramework.Core.Abstractions/lifecycle/ILifecycle.cs new file mode 100644 index 0000000..b3bb67f --- /dev/null +++ b/GFramework.Core.Abstractions/lifecycle/ILifecycle.cs @@ -0,0 +1,6 @@ +namespace GFramework.Core.Abstractions.lifecycle; + +/// +/// 完整生命周期接口,组合了初始化和销毁能力 +/// +public interface ILifecycle : IInitializable, IDisposable; \ No newline at end of file diff --git a/GFramework.Core.Abstractions/model/IModel.cs b/GFramework.Core.Abstractions/model/IModel.cs index 9fb2e52..4ec22d0 100644 --- a/GFramework.Core.Abstractions/model/IModel.cs +++ b/GFramework.Core.Abstractions/model/IModel.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.lifecycle; using GFramework.Core.Abstractions.rule; namespace GFramework.Core.Abstractions.model; @@ -6,10 +7,4 @@ namespace GFramework.Core.Abstractions.model; /// /// 模型接口,定义了模型的基本行为和功能 /// -public interface IModel : IContextAware, IArchitecturePhaseAware -{ - /// - /// 初始化模型 - /// - void Init(); -} \ No newline at end of file +public interface IModel : IContextAware, IArchitecturePhaseAware, IInitializable; \ No newline at end of file diff --git a/GFramework.Core.Abstractions/system/ISystem.cs b/GFramework.Core.Abstractions/system/ISystem.cs index ae2a3b2..7a8b415 100644 --- a/GFramework.Core.Abstractions/system/ISystem.cs +++ b/GFramework.Core.Abstractions/system/ISystem.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.architecture; +using GFramework.Core.Abstractions.lifecycle; using GFramework.Core.Abstractions.rule; namespace GFramework.Core.Abstractions.system; @@ -7,17 +8,4 @@ namespace GFramework.Core.Abstractions.system; /// 系统接口,定义了系统的基本行为和功能 /// 该接口继承了多个框架相关的接口,提供了系统初始化和销毁能力 /// -public interface ISystem : IContextAware, IArchitecturePhaseAware -{ - /// - /// 初始化系统 - /// 在系统被创建后调用,用于执行系统的初始化逻辑 - /// - void Init(); - - /// - /// 销毁系统 - /// 在系统被销毁前调用,用于执行系统的资源清理逻辑 - /// - void Destroy(); -} \ No newline at end of file +public interface ISystem : IContextAware, IArchitecturePhaseAware, ILifecycle; \ No newline at end of file diff --git a/GFramework.Core.Abstractions/utility/IContextUtility.cs b/GFramework.Core.Abstractions/utility/IContextUtility.cs index 245aba7..93641b8 100644 --- a/GFramework.Core.Abstractions/utility/IContextUtility.cs +++ b/GFramework.Core.Abstractions/utility/IContextUtility.cs @@ -1,4 +1,5 @@ -using GFramework.Core.Abstractions.rule; +using GFramework.Core.Abstractions.lifecycle; +using GFramework.Core.Abstractions.rule; namespace GFramework.Core.Abstractions.utility; @@ -6,10 +7,4 @@ namespace GFramework.Core.Abstractions.utility; /// 上下文工具接口,继承自IUtility和IContextAware接口 /// 提供具有上下文感知能力的工具功能 /// -public interface IContextUtility : IUtility, IContextAware -{ - /// - /// 初始化上下文工具 - /// - void Init(); -} \ No newline at end of file +public interface IContextUtility : IUtility, IContextAware, ILifecycle; \ No newline at end of file diff --git a/GFramework.Core/model/AbstractModel.cs b/GFramework.Core/model/AbstractModel.cs index 5d67067..3968d03 100644 --- a/GFramework.Core/model/AbstractModel.cs +++ b/GFramework.Core/model/AbstractModel.cs @@ -1,4 +1,5 @@ using GFramework.Core.Abstractions.enums; +using GFramework.Core.Abstractions.lifecycle; using GFramework.Core.Abstractions.model; using GFramework.Core.rule; @@ -12,7 +13,7 @@ public abstract class AbstractModel : ContextAwareBase, IModel /// /// 初始化模型,调用抽象方法OnInit执行具体初始化逻辑 /// - void IModel.Init() + void IInitializable.Init() { OnInit(); } diff --git a/GFramework.Core/system/AbstractSystem.cs b/GFramework.Core/system/AbstractSystem.cs index ac97996..da1d4bf 100644 --- a/GFramework.Core/system/AbstractSystem.cs +++ b/GFramework.Core/system/AbstractSystem.cs @@ -17,7 +17,7 @@ public abstract class AbstractSystem : ContextAwareBase, ISystem /// /// 系统初始化方法,调用抽象初始化方法 /// - void ISystem.Init() + public void Init() { var name = GetType().Name; _logger = LoggerFactoryResolver.Provider.CreateLogger(name); @@ -31,7 +31,7 @@ public abstract class AbstractSystem : ContextAwareBase, ISystem /// /// 系统销毁方法,调用抽象销毁方法 /// - void ISystem.Destroy() + public void Destroy() { _logger.Debug($"Destroying system: {GetType().Name}"); diff --git a/GFramework.Core/utility/AbstractContextUtility.cs b/GFramework.Core/utility/AbstractContextUtility.cs index d5d9094..5e64b6e 100644 --- a/GFramework.Core/utility/AbstractContextUtility.cs +++ b/GFramework.Core/utility/AbstractContextUtility.cs @@ -16,7 +16,7 @@ public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility /// /// 初始化上下文工具类 /// - void IContextUtility.Init() + public void Init() { var name = GetType().Name; // 获取上下文中的日志记录器 @@ -30,8 +30,26 @@ public abstract class AbstractContextUtility : ContextAwareBase, IContextUtility Logger.Info($"Context Utility initialized: {name}"); } + /// + /// 销毁上下文工具类实例 + /// + public void Destroy() + { + var name = GetType().Name; + Logger.Debug($"Destroying Context Utility: {name}"); + OnDestroy(); + Logger.Info($"Context Utility destroyed: {name}"); + } + /// /// 抽象初始化方法,由子类实现具体的初始化逻辑 /// protected abstract void OnInit(); + + /// + /// 虚拟销毁方法,可由子类重写以实现自定义销毁逻辑 + /// + protected virtual void OnDestroy() + { + } } \ No newline at end of file