From ce6cb3f8df25420eab56b40f6ee5a7002bfc12d6 Mon Sep 17 00:00:00 2001
From: GeWuYou <95328647+GeWuYou@users.noreply.github.com>
Date: Sat, 17 Jan 2026 13:20:33 +0800
Subject: [PATCH] =?UTF-8?q?refactor(state):=20=E4=BF=AE=E6=94=B9=E7=8A=B6?=
=?UTF-8?q?=E6=80=81=E6=9C=BA=E6=8E=A5=E5=8F=A3=E8=BF=94=E5=9B=9E=E7=B1=BB?=
=?UTF-8?q?=E5=9E=8B=E6=94=AF=E6=8C=81=E9=93=BE=E5=BC=8F=E8=B0=83=E7=94=A8?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
- 将Register方法返回类型从void改为IStateMachine
- 将Unregister方法返回类型从void改为IStateMachine
- 在实现类中添加return this语句
- 更新接口定义以匹配新的返回类型
- 实现链式调用功能提升API易用性
---
GFramework.Core.Abstractions/state/IStateMachine.cs | 4 ++--
GFramework.Core/state/StateMachine.cs | 10 +++++++---
2 files changed, 9 insertions(+), 5 deletions(-)
diff --git a/GFramework.Core.Abstractions/state/IStateMachine.cs b/GFramework.Core.Abstractions/state/IStateMachine.cs
index 7922b63..1e36c85 100644
--- a/GFramework.Core.Abstractions/state/IStateMachine.cs
+++ b/GFramework.Core.Abstractions/state/IStateMachine.cs
@@ -17,13 +17,13 @@ public interface IStateMachine
/// 注册一个状态到状态机中
///
/// 要注册的状态实例
- void Register(IState state);
+ IStateMachine Register(IState state);
///
/// 从状态机中注销指定类型的状态
///
/// 要注销的状态类型,必须实现IState接口
- void Unregister() where T : IState;
+ IStateMachine Unregister() where T : IState;
///
/// 检查是否可以切换到指定类型的状态
diff --git a/GFramework.Core/state/StateMachine.cs b/GFramework.Core/state/StateMachine.cs
index a2f1a0a..7a270ed 100644
--- a/GFramework.Core/state/StateMachine.cs
+++ b/GFramework.Core/state/StateMachine.cs
@@ -24,24 +24,26 @@ public class StateMachine(int maxHistorySize = 10) : IStateMachine
/// 注册一个状态到状态机中
///
/// 要注册的状态实例
- public void Register(IState state)
+ public IStateMachine Register(IState state)
{
lock (_lock)
{
States[state.GetType()] = state;
}
+
+ return this;
}
///
/// 从状态机中注销指定类型的状态
///
/// 要注销的状态类型
- public void Unregister() where T : IState
+ public IStateMachine Unregister() where T : IState
{
lock (_lock)
{
var type = typeof(T);
- if (!States.TryGetValue(type, out var state)) return;
+ if (!States.TryGetValue(type, out var state)) return this;
// 如果当前状态是要注销的状态,则先执行退出逻辑
if (Current == state)
@@ -60,6 +62,8 @@ public class StateMachine(int maxHistorySize = 10) : IStateMachine
States.Remove(type);
}
+
+ return this;
}
///