mirror of
https://github.com/GeWuYou/GFramework.git
synced 2026-03-22 19:03:29 +08:00
- 新增 Architecture 基类与 IArchitecture 接口,实现单例模式与组件注册管理 - 集成 IOC 容器支持系统、模型、工具的依赖注入与生命周期管理 - 实现命令模式基础类 AbstractCommand 与接口 ICommand,支持带返回值命令 - 提供事件系统集成,支持事件的发布与订阅机制 - 添加控制器接口 IController,整合命令发送、事件注册与模型获取能力 - 创建详细的 README 文档说明各组件使用方式与设计模式应用 - 支持命令、查询、事件的统一调度与解耦通信机制
68 lines
2.5 KiB
C#
68 lines
2.5 KiB
C#
namespace GFramework.framework.events;
|
||
|
||
/// <summary>
|
||
/// EasyEvents事件管理器类,用于全局事件的注册、获取和管理
|
||
/// 提供了类型安全的事件系统,支持泛型事件的自动创建和检索
|
||
/// </summary>
|
||
public class EasyEvents
|
||
{
|
||
/// <summary>
|
||
/// 全局单例事件管理器实例
|
||
/// </summary>
|
||
private static readonly EasyEvents MGlobalEvents = new();
|
||
|
||
/// <summary>
|
||
/// 获取指定类型的全局事件实例
|
||
/// </summary>
|
||
/// <typeparam name="T">事件类型,必须实现IEasyEvent接口</typeparam>
|
||
/// <returns>指定类型的事件实例,如果未注册则返回默认值</returns>
|
||
public static T Get<T>() where T : IEasyEvent => MGlobalEvents.GetEvent<T>();
|
||
|
||
/// <summary>
|
||
/// 注册指定类型的全局事件
|
||
/// </summary>
|
||
/// <typeparam name="T">事件类型,必须实现IEasyEvent接口且具有无参构造函数</typeparam>
|
||
public static void Register<T>() where T : IEasyEvent, new() => MGlobalEvents.AddEvent<T>();
|
||
|
||
/// <summary>
|
||
/// 存储事件类型与事件实例映射关系的字典
|
||
/// </summary>
|
||
private readonly Dictionary<Type, IEasyEvent> _mTypeEvents = new();
|
||
|
||
/// <summary>
|
||
/// 添加指定类型的事件到事件字典中
|
||
/// </summary>
|
||
/// <typeparam name="T">事件类型,必须实现IEasyEvent接口且具有无参构造函数</typeparam>
|
||
public void AddEvent<T>() where T : IEasyEvent, new() => _mTypeEvents.Add(typeof(T), new T());
|
||
|
||
/// <summary>
|
||
/// 获取指定类型的事件实例
|
||
/// </summary>
|
||
/// <typeparam name="T">事件类型,必须实现IEasyEvent接口</typeparam>
|
||
/// <returns>指定类型的事件实例,如果不存在则返回默认值</returns>
|
||
public T GetEvent<T>() where T : IEasyEvent
|
||
{
|
||
return _mTypeEvents.TryGetValue(typeof(T), out var e) ? (T)e : default;
|
||
}
|
||
|
||
/// <summary>
|
||
/// 获取指定类型的事件实例,如果不存在则创建并添加到事件字典中
|
||
/// </summary>
|
||
/// <typeparam name="T">事件类型,必须实现IEasyEvent接口且具有无参构造函数</typeparam>
|
||
/// <returns>指定类型的事件实例</returns>
|
||
public T GetOrAddEvent<T>() where T : IEasyEvent, new()
|
||
{
|
||
var eType = typeof(T);
|
||
// 尝试从字典中获取事件实例
|
||
if (_mTypeEvents.TryGetValue(eType, out var e))
|
||
{
|
||
return (T)e;
|
||
}
|
||
|
||
// 如果不存在则创建新实例并添加到字典中
|
||
var t = new T();
|
||
_mTypeEvents.Add(eType, t);
|
||
return t;
|
||
}
|
||
}
|