• QFramework引入Event


    问题:

    上篇我们通过引入了 Command 来帮助 Controller 分担了一部分的交互逻辑。

    但是表现逻辑的代码目前看起来并不是很智能。

    1. // 监听输入
    2. mBtnAdd.onClick.AddListener(() =>
    3. {
    4. // 交互逻辑
    5. this.SendCommand();
    6. // 表现逻辑
    7. UpdateView();
    8. });
    9. mBtnSub.onClick.AddListener(() =>
    10. {
    11. // 交互逻辑
    12. this.SendCommand();
    13. // 表现逻辑
    14. UpdateView();
    15. });

    每次调用逻辑之后,表现逻辑部分都需要手动调用一次(UpdateView 方法)。

    在一个项目中,表现逻辑的调用次数,至少会和交互逻辑的调用次数一样多。因为只要修改了数据,对应地就要把数据的biang在界面上表现出来。

    而这部分嗲用表现逻辑的代码也会很多,所以我们引入一个事件机制来解决这个问题。

    这个事件机制的使用其实是和 Command 一起使用的。

    即通过 Command 修改数据,当数据发生修改后发送对应的数据变更事件。

    这个是简化版本的 CQRS 原则,即 Command Query Responsibility Separiation,读写分离原则。

    引入这项原则会很容易实现 事件驱动、数据驱动 架构。


    解决问题:

    让我们用QFramework来实现一下:

    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. // 1. 定义一个 Model 对象
    4. public class CounterAppModel : AbstractModel
    5. {
    6. public int Count;
    7. protected override void OnInit()
    8. {
    9. Count = 0;
    10. }
    11. }
    12. // 2.定义一个架构(提供 MVC、分层、模块管理等)
    13. public class CounterApp : Architecture<CounterApp>
    14. {
    15. protected override void Init()
    16. {
    17. // 注册 Model
    18. this.RegisterModel(new CounterAppModel());
    19. }
    20. }
    21. // 定义数据变更事件
    22. public struct CountChangeEvent // ++
    23. {
    24. }
    25. // 引入 Command
    26. public class IncreaseCountCommand : AbstractCommand
    27. {
    28. protected override void OnExecute()
    29. {
    30. this.GetModel().Count++;
    31. this.SendEvent(); // ++
    32. }
    33. }
    34. public class DecreaseCountCommand : AbstractCommand
    35. {
    36. protected override void OnExecute()
    37. {
    38. this.GetModel().Count--;
    39. this.SendEvent(); // ++
    40. }
    41. }
    42. // Controller
    43. public class CounterAppController : MonoBehaviour , IController /* 3.实现 IController 接口 */
    44. {
    45. // View
    46. private Button mBtnAdd;
    47. private Button mBtnSub;
    48. private Text mCountText;
    49. // 4. Model
    50. private CounterAppModel mModel;
    51. void Start()
    52. {
    53. // 5. 获取模型
    54. mModel = this.GetModel();
    55. // View 组件获取
    56. mBtnAdd = transform.Find("BtnAdd").GetComponent
    57. mBtnSub = transform.Find("BtnSub").GetComponent
    58. mCountText = transform.Find("CountText").GetComponent();
    59. // 监听输入
    60. mBtnAdd.onClick.AddListener(() =>
    61. {
    62. // 交互逻辑
    63. this.SendCommand();
    64. });
    65. mBtnSub.onClick.AddListener(() =>
    66. {
    67. // 交互逻辑
    68. this.SendCommand(new DecreaseCountCommand(/* 这里可以传参(如果有) */));
    69. });
    70. UpdateView();
    71. // 表现逻辑
    72. this.RegisterEvent(e =>
    73. {
    74. UpdateView();
    75. }).UnRegisterWhenGameObjectDestroyed(gameObject);
    76. }
    77. void UpdateView()
    78. {
    79. mCountText.text = mModel.Count.ToString();
    80. }
    81. // 3.
    82. public IArchitecture GetArchitecture()
    83. {
    84. return CounterApp.Interface;
    85. }
    86. private void OnDestroy()
    87. {
    88. // 8. 将 Model 设置为空
    89. mModel = null;
    90. }
    91. }

    引入事件机制 和 CQRS 原则之后,我们的表现逻辑的代码变少了很多。 由原来的两次主动调用,变成了一处监听事件,接收事件进行调用。这样减缓了很多交互逻辑。而 QFramework 所提供的概念中,最重要的概念已经接触到了,即 CQRS,通过 Command 去修改数据,数据发生修改后发送数据变更事件。


     

    总结:

    到这里,对于 QFramework 架构的使用算是真正的入门了。接下来我们讲一下如何引入Utility以及System

  • 相关阅读:
    【Android】MediaPlayer生命周期
    使用免费SSL证书的好处
    web初识
    给你一个大厂面试的机会,你能面试上吗?进来看看!
    C语言实现快速排序算法
    hive从入门到放弃(六)——常用文件存储格式
    【斗罗大陆2】动画新增12集备案,冰碧帝皇蝎形象被吐槽遭狂喷!
    【java核心技术】Java知识总结 -- 接口
    python每日学5:python工程(大型项目)的组织架构:包、模块、类、方法
    maya遇到渲染慢渲染卡顿问题怎么办?
  • 原文地址:https://blog.csdn.net/LiKe11807/article/details/126490039