编辑代码

using System;

// Controller
namespace QFramework.Example
{

    // 定义一个Model对象
    public class CounterAppModel : AbstractModel
    {
        private int mCount  // 实现存储功能
        //public int Count;  // 实现存储功能需要修改
        public int Count
        {
            get => mCount;
            set
            {
                if(mCount != value)
                {
                    mCount = value;
                    PlayerPrefs.SetInt(nameof(Count),mCount);
                }
            }
        }
        protected override void OnInit()
        {
            //Count = 0;   //实现存储功能需要修改
            var storage = this.GetUtility<Storage>();
            Count = storage.LoadInt(nameof(Count));
            // 可以通过 CountApp.Interface 监听数据变更事件
            CountApp.Interface.RegisterEvent<CountChangeEvent>(e =>
            {
                this.GetUtility<Storage>().SaveInt(nameof(Count), Count);
            });
        }
    }

    // 定义 utility 层
    public class Storage : IUtility
    {
        public void SaveInt(string key, int value)
        {
            PlayerPrefs.SetInt(key, value);
        }

        public int LoadInt(string key, int defaultValue = 0)
        {
            return PlayerPrefs.GetInt(key, defaultValue);
        }
    }

    // 定义一个架构(提供 MVC,分层,模块管理等)
    public class CounterApp : Architecture<CounterApp>
    {
        protected override void Init()
        {
            // 注册 Model
            this.RegisterModel(new CounterAppModel());
            // Utility相关  注册存储工具的对象
            this.RegisterUtility(new Storage());
        }
    }

    // 定义数据变更事件
    public struct CountChangeEvent
    {

    }

    // 引入 Command
    public class IncreaseCountCommand : AbstractCommand
    {
        protected override void OnExecute()
        {
            this.GetModel<CounterAppModel>().Count++;
            this.SendEvent<CountChangeEvent>(); //++
        }
    }

    public class DecreaseCountCommand : AbstractCommand
    {
        protected override void OnExecute()
        {
            this.GetModel<CountAppModel>().Count--;
            this.SendEvent<CountChangeEvent>(); //++
        }
    }

    // Controller
    public class CounterAppController : MonoBehaviour, IController /* 实现 IController 接口 */
    {
        // View
        private Button mBtnAdd;
        private Button mBtnSub;
        private Text mCountText;

        // Model
        private CounterAppModel mModel;

        void Start()
        {
            // 获取模型
            mModel = this.GetModel<CounterAppModel>();

            // view 组件获取
            mBtnAdd = transform.Find("BtnAdd").GetComponent<Button>();
            mBtnSub = transform.Find("BtnSub").GetComponent<Button>();
            mCountText = transform.Find("CounterText").GetComponent<Text>();
            
            // 监听输入
            mBtnAdd.onClick.AddListener(() = > 
            {
                // 交互逻辑
                this.SendCommand<IncreaseCountCommand>();  //mCount++;
                // 表现逻辑
                //UpdateView();
            });

            mBtnSub.onClick.AddListener(() = > 
            {
                // 交互逻辑
                this.SendCommand(new DecreaseCountCommand>()); //mCount--;
                // 表现逻辑
                //UpdateView();
            });   

            UpdateView();

            // 表现逻辑
            this.RegisterEvent<CountChangeEvent>(e => 
            {
                UpdateView();
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        void UpdateView()
        {
            mCountText.text = mCount.ToString();
        }

        // 指定架构
        public IArchitecture GetArchitecture()
        {
            return CounterApp.Interface;
        }

        private void OnDestroy()
        {
            // 将 Model 置空
            mModel = null;
        }
    }
}