QFramework框架 Architecture:Sysrem模块案例

    #region Architecture
    // 定义一个架构接口,包含注册和获取系统、模型、工具,发送命令和事件,以及注销功能。
    public interface IArchitecture
    {
        void RegisterSystem<T>(T system) where T : ISystem;
        void RegisterModel<T>(T model) where T : IModel;
        void RegisterUtility<T>(T utility) where T : IUtility;
        T GetSystem<T>() where T : class, ISystem;
        T GetModel<T>() where T : class, IModel;
        T GetUtility<T>() where T : class, IUtility;
        void SendCommand<T>(T command) where T : ICommand;
        TResult SendCommand<TResult>(ICommand<TResult> command);
        TResult SendQuery<TResult>(IQuery<TResult> query);
        void SendEvent<T>() where T : new();
        void SendEvent<T>(T e);
        IUnRegister RegisterEvent<T>(Action<T> onEvent);
        void UnRegisterEvent<T>(Action<T> onEvent);
        void Deinit();
    }

    // 抽象类,实现IArchitecture接口,使用泛型确保单例模式。
    public abstract class Architecture<T> : IArchitecture where T : Architecture<T>, new()
    {
        private bool mInited = false; // 标记架构是否已初始化

        public static Action<T> OnRegisterPatch = architecture => { }; // 静态委托,用于注册额外的初始化操作

        protected static T mArchitecture; // 静态字段,存储单例实例

        // 静态属性,确保架构实例化并初始化
        public static IArchitecture Interface
        {
            get
            {
                if (mArchitecture == null) MakeSureArchitecture();
                return mArchitecture;
            }
        }

        // 静态方法,确保架构实例化并执行初始化操作
        static void MakeSureArchitecture()
        {
            if (mArchitecture == null)
            {
                mArchitecture = new T();
                mArchitecture.Init();
                OnRegisterPatch?.Invoke(mArchitecture);
                foreach (var model in mArchitecture.mContainer.GetInstancesByType<IModel>().Where(m => !m.Initialized))
                {
                    model.Init();
                    model.Initialized = true;
                }
                foreach (var system in mArchitecture.mContainer.GetInstancesByType<ISystem>().Where(m => !m.Initialized))
                {
                    system.Init();
                    system.Initialized = true;
                }
                mArchitecture.mInited = true;
            }
        }

        protected abstract void Init(); // 抽象方法,子类必须实现以提供初始化逻辑

        // 注销方法,清理资源
        public void Deinit()
        {
            OnDeinit();
            foreach (var system in mContainer.GetInstancesByType<ISystem>().Where(s => s.Initialized)) system.Deinit();
            foreach (var model in mContainer.GetInstancesByType<IModel>().Where(m => m.Initialized)) model.Deinit();
            mContainer.Clear();
            mArchitecture = null;
        }

        protected virtual void OnDeinit() { } // 可重写的注销方法

        private IOCContainer mContainer = new IOCContainer(); // IOC容器,用于依赖注入

        // 注册系统,如果已初始化则立即初始化系统
        public void RegisterSystem<TSystem>(TSystem system) where TSystem : ISystem
        {
            system.SetArchitecture(this);
            mContainer.Register<TSystem>(system);
            if (mInited)
            {
                system.Init();
                system.Initialized = true;
            }
        }

        // 注册模型,如果已初始化则立即初始化模型
        public void RegisterModel<TModel>(TModel model) where TModel : IModel
        {
            model.SetArchitecture(this);
            mContainer.Register<TModel>(model);
            if (mInited)
            {
                model.Init();
                model.Initialized = true;
            }
        }

        // 注册工具
        public void RegisterUtility<TUtility>(TUtility utility) where TUtility : IUtility =>
            mContainer.Register<TUtility>(utility);

        // 获取系统实例
        public TSystem GetSystem<TSystem>() where TSystem : class, ISystem => mContainer.Get<TSystem>();

        // 获取模型实例
        public TModel GetModel<TModel>() where TModel : class, IModel => mContainer.Get<TModel>();

        // 获取工具实例
        public TUtility GetUtility<TUtility>() where TUtility : class, IUtility => mContainer.Get<TUtility>();

        // 执行命令并返回结果
        public TResult SendCommand<TResult>(ICommand<TResult> command) => ExecuteCommand(command);

        // 执行命令无返回结果
        public void SendCommand<TCommand>(TCommand command) where TCommand : ICommand => ExecuteCommand(command);

        // 执行命令的具体实现,设置架构并执行
        protected virtual TResult ExecuteCommand<TResult>(ICommand<TResult> command)
        {
            command.SetArchitecture(this);
            return command.Execute();
        }

        // 执行无返回结果的命令的具体实现
        protected virtual void ExecuteCommand(ICommand command)
        {
            command.SetArchitecture(this);
            command.Execute();
        }

        // 发送查询并返回结果
        public TResult SendQuery<TResult>(IQuery<TResult> query) => DoQuery<TResult>(query);

        // 执行查询的具体实现
        protected virtual TResult DoQuery<TResult>(IQuery<TResult> query)
        {
            query.SetArchitecture(this);
            return query.Do();
        }

        // 事件系统实例
        private TypeEventSystem mTypeEventSystem = new TypeEventSystem();

        // 发送无参数事件
        public void SendEvent<TEvent>() where TEvent : new() => mTypeEventSystem.Send<TEvent>();

        // 发送带参数事件
        public void SendEvent<TEvent>(TEvent e) => mTypeEventSystem.Send<TEvent>(e);

        // 注册事件监听
        public IUnRegister RegisterEvent<TEvent>(Action<TEvent> onEvent) => mTypeEventSystem.Register<TEvent>(onEvent);

        // 注销事件监听
        public void UnRegisterEvent<TEvent>(Action<TEvent> onEvent) => mTypeEventSystem.UnRegister<TEvent>(onEvent);
    }

    // 定义事件接口,用于事件监听
    public interface IOnEvent<T>
    {
        void OnEvent(T e);
    }

    // 全局事件扩展,允许注册和注销全局事件
    public static class OnGlobalEventExtension
    {
        public static IUnRegister RegisterEvent<T>(this IOnEvent<T> self) where T : struct =>
            TypeEventSystem.Global.Register<T>(self.OnEvent);

        public static void UnRegisterEvent<T>(this IOnEvent<T> self) where T : struct =>
            TypeEventSystem.Global.UnRegister<T>(self.OnEvent);
    }

    #endregion
#region System

// 定义系统接口,继承多个功能接口,包括属于架构、设置架构、获取模型、获取工具、注册事件、发送事件、获取系统和初始化功能。
public interface ISystem : IBelongToArchitecture, ICanSetArchitecture, ICanGetModel, ICanGetUtility,
    ICanRegisterEvent, ICanSendEvent, ICanGetSystem, ICanInit
{
}
// 抽象类实现ISystem接口,提供系统组件的基本功能和生命周期管理。
public abstract class AbstractSystem : ISystem
{
    private IArchitecture mArchitecture; // 私有字段,存储系统所属的架构引用。

    // 实现接口,返回系统所属的架构。
    IArchitecture IBelongToArchitecture.GetArchitecture() => mArchitecture;

    // 实现接口,设置系统所属的架构。
    void ICanSetArchitecture.SetArchitecture(IArchitecture architecture) => mArchitecture = architecture;

    public bool Initialized { get; set; } // 公共属性,标记系统是否已初始化。

    // 实现接口,调用抽象方法OnInit进行初始化。
    void ICanInit.Init() => OnInit();

    // 公共方法,调用受保护的虚方法OnDeinit进行反初始化。
    public void Deinit() => OnDeinit();
    protected virtual void OnDeinit() { } // 受保护的虚方法,可被子类重写以提供反初始化逻辑。
    protected abstract void OnInit(); // 抽象方法,子类必须实现以提供初始化逻辑。
}

#endregion

以上为源码

当然可以。以下是如何在实际项目中使用 ISystem 和 AbstractSystem 的一个简单示例。假设我们正在开发一个游戏,我们需要一个系统来管理游戏中的所有角色(例如玩家和敌人)的状态。

首先,我们定义一个具体的系统类 CharacterSystem,它继承自 AbstractSystem 并实现必要的抽象方法 OnInit 和 OnDeinit。

public class CharacterSystem : AbstractSystem
{
    private List<Character> characters = new List<Character>();

    // 初始化方法,设置系统启动时的配置或状态
    protected override void OnInit()
    {
        // 可以在这里加载角色,或设置初始状态
        Console.WriteLine("Character System Initialized");
    }

    // 反初始化方法,清理资源或状态
    protected override void OnDeinit()
    {
        // 清理角色列表或其他资源
        characters.Clear();
        Console.WriteLine("Character System Deinitialized");
    }

    // 添加角色到系统
    public void AddCharacter(Character character)
    {
        characters.Add(character);
        Console.WriteLine("Character added");
    }

    // 移除角色
    public void RemoveCharacter(Character character)
    {
        characters.Remove(character);
        Console.WriteLine("Character removed");
    }
}

public class GameArchitecture : Architecture<GameArchitecture>
{
    protected override void Init()
    {
        // 注册系统
        RegisterSystem(new CharacterSystem());
    }
}

class Program
{
    static void Main(string[] args)
    {
        // 获取架构实例
        var architecture = GameArchitecture.Interface;

        // 获取CharacterSystem实例并使用
        var characterSystem = architecture.GetSystem<CharacterSystem>();
        characterSystem.Init();  // 显式调用初始化(如果在注册时未自动初始化)

        // 添加角色
        characterSystem.AddCharacter(new Character("Hero"));
        characterSystem.AddCharacter(new Character("Enemy"));

        // 业务逻辑处理...

        // 清理
        characterSystem.Deinit();
    }
}

public class Character
{
    public string Name { get; set; }

    public Character(string name)
    {
        Name = name;
    }
}

CharacterSystem 负责管理游戏中的所有角色。它在初始化时设置初始状态,在反初始化时清理资源。通过 GameArchitecture 类注册到架构中,可以在游戏的任何地方通过架构访问和操作角色系统。这种方式使得系统的管理更加模块化和灵活。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值