using System; using System.Collections.Generic; using UnityEngine; namespace QFramework.Example { public class ArchitectureInHeartExample : MonoBehaviour { #region Framework public interface ICommand { void Execute(); } public class BindableProperty { private T mValue = default; public T Value { get => mValue; set { if (mValue != null && mValue.Equals(value)) return; mValue = value; OnValueChanged?.Invoke(mValue); } } public event Action OnValueChanged = _ => { }; } #endregion #region 定义 Model public static class CounterModel { public static BindableProperty Counter = new BindableProperty() { Value = 0 }; } #endregion #region 定义 Command public struct IncreaseCountCommand : ICommand { public void Execute() { CounterModel.Counter.Value++; } } public struct DecreaseCountCommand : ICommand { public void Execute() { CounterModel.Counter.Value--; } } #endregion private void OnGUI() { if (GUILayout.Button("+")) { new IncreaseCountCommand().Execute(); } GUILayout.Label(CounterModel.Counter.Value.ToString()); if (GUILayout.Button("-")) { new DecreaseCountCommand().Execute(); } } } }