個人感覺有點像是觀察者模式
Store是通用的? 其他的都是根據自己項目進行更改的類和方法

using UnityEngine;
using QFramework;
// 1.請在菜單 編輯器擴充/Namespace Settings 裡設定命名空間
// 2.命名空間更改後,生成代碼之後,需要把邏輯代碼檔案(非 Designer)的命名空間手動更改
namespace QFramework.Example
{
public partial class LoginPanelPanel : ViewController
{
private void Awake()
{
Store<APPState>.SetReducer(AppRedcuer.Reduce);
Store<APPState>.Subscribe += (state) => { Debug.Log(state.RegisterSucced);};
}
void Start()
{
// Code Here
loginButton.onClick.AddListener(() =>
{
var username = userName.text;
var passWord = password.text;
if (!string.IsNullOrEmpty(username) &&
!string.IsNullOrEmpty(passWord))
{
Store<APPState>.Dispatch(new RegisterAction()
{
Username = username,
Password = passWord,
Email = username + "@163.com"
});
Debug.Log("請求注冊接口");
}
});
}
}
}
action的代碼如下
namespace QFramework.Example
{
public class RegisterAction
{
public string Email;
public string Username;
public string Password;
}
}
傳遞到Store類中
using System;
namespace QFramework.Example
{
public static class Store<T> where T : new()
{
private static Func<T, object, T> mReducerFunc;
public static void SetReducer(Func<T, object, T> ReducerFunc)
{
mReducerFunc = ReducerFunc;
}
public static T mState = new T();
public static T State
{
get { return mState; }
}
public static Action<T> Subscribe = (state => { UnityEngine.Debug.Log("123"); });
public static void Dispatch(object action)
{
mState = mReducerFunc(mState, action);
Subscribe(mState);
}
}
}
namespace QFramework.Example
{
public class APPState
{
public bool RegisterSucced;
}
}
using UnityEngine;
namespace QFramework.Example
{
public class AppRedcuer
{
public static APPState Reduce(APPState previousState, object action)
{
if (action is RegisterAction)
{
var registerAction = action as RegisterAction;
Debug.LogFormat("{0},{1},{2}", registerAction.Email, registerAction.Username, registerAction.Password);
previousState.RegisterSucced = true;
}
return previousState;
}
}
}