# 1. AbstractArchitecture
一个项目需要创建一个类,这个类需要继承AbstractArchitecture,用于注册System、Model、Utility,所有创建的都必须在Init()方法中注册,否则无法获取
ps:目前一个项目只能有一个类继承AbstractArchitecture
示例 代码
using UnityEngine;
namespace BlueVersion2
{
public class TestApp : AbstractArchitecture<TestApp>
{
protected override void Init()
{
// 注册 System
this.RegisterSystem<ITestSystem>(new TestSystem());
this.RegisterModel<ITestModel>(new TestModel());
}
}
}
建议使用方式:定义一个Model类型的接口,继承IModel,然后创建Model层类继承定义的Model类型的接口;
做法的意义:相同的数据在不同测试环境下数据处理不同,例如测试环境1数据++,测试环境2数据--。定义接口后继承只需要在TestApp类中更改注册的类即可
this.RegisterModel(new TestModel1());
this.RegisterModel(new TestModel2());
OnInit()用于初始化,例如给字段赋值等
示例 代码
namespace BlueVersion2
{
public interface ITestModel : IModel
{
int TestQuery { get; }
BindableProperty<int> Count { get; }
}
public class TestModel : ITestModel
{
public int TestQuery { get; set; }
public BindableProperty<int> Count { get; } = new BindableProperty<int>(5);
public void OnInit()
{
TestQuery = 5;
}
}
}
namespace BlueVersion2
{
public interface ITestSystem : ISystem
{
BindableProperty<int> Count { get; }
}
public class TestSystem : ITestSystem
{
public BindableProperty<int> Count { get; } = new BindableProperty<int>(5);
public void Init()
{
}
}
}
Command需要继承ICommand接口
需要返回值继承ICommand接口
不限定Class、Struct
需要执行的方法写在OnExcute()中
示例 代码
// 定义Command
using System;
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
namespace BlueVersion2
{
public class AddCommand : ICommand
{
public void OnExcute()
{
this.GetModel<ITestModel>().Count.Value++;
}
}
public class SubCommand : ICommand
{
public void OnExcute()
{
this.GetSystem<ITestSystem>().Count.Value--;
}
}
public class TaskACommand : ICommand<Task<bool>>
{
public async Task<bool> OnExcute()
{
await Task.Delay(TimeSpan.FromSeconds(2.0f));
Debug.Log("等待了2秒");
return true;
}
}
public class SimpleResultCommand : ICommand<string>
{
public string OnExcute()
{
return "Hello Command With Result";
}
}
public class CoroutineCommand : ICommand<IEnumerator>
{
public IEnumerator OnExcute()
{
Debug.Log("ACoroutineCommandStart:" + Time.time);
yield return new WaitForSeconds(1.0f);
Debug.Log("ACoroutineCommandFinish:" + Time.time);
}
}
}
// 发送Command
using System.Collections;
using System.Threading.Tasks;
using UnityEngine;
using UnityEngine.UI;
namespace BlueVersion2
{
public class Test : MonoBehaviour, IController
{
public Button btnAdd;
public Button btnSub;
public Button btnTaskACommand;
public Button btnSimpleResultCommand;
public Button btnCoroutineCommand;
public Text TestText;
private void Start()
{
SubCommand SubCommand = new SubCommand();
btnAdd.onClick.AddListener(() =>
{
this.SendCommand<AddCommand>();
});
btnSub.onClick.AddListener(() =>
{
this.SendCommand(SubCommand);
});
btnTaskACommand.onClick.AddListener(async () =>
{
var result = await this.SendCommand(new TaskACommand());
Debug.Log($"已经执行TestCommand+{result}");
});
btnSimpleResultCommand.onClick.AddListener(() =>
{
string str = this.SendCommand(new SimpleResultCommand());
Debug.LogError(str);
});
btnCoroutineCommand.onClick.AddListener(() =>
{
IEnumerator coro = this.SendCommand(new CoroutineCommand());
StartCoroutine(coro);
});
}
}
Event需要继承IEvent接口,不限定Class、Struct
可以在Command中发送事件
可以在Controller层中进行监听
示例 代码
//定义事件
namespace BlueVersion2
{
public struct TestEvent : IEvent
{
}
}
// 监听事件
using UnityEngine;
namespace BlueVersion2
{
public class Test : MonoBehaviour, IController
{
private void Start()
{
this.RegisterEvent<TestEvent>(TestEventListener); // 监听事件
}
private void TestEventListener(TestEvent e)
{
Debug.Log($"事件触发,事件名:{e.GetType()}");
}
}
}
// 发送事件
namespace BlueVersion2
{
public class SubCommand : ICommand
{
public void Execute()
{
this.SendEvent<TestEvent>();
}
}
}
// 定义BindableProperty
namespace BlueVersion2
{
public interface ITestSystem : ISystem
{
BindableProperty<int> Count { get; }
}
public class TestSystem : ITestSystem
{
public BindableProperty<int> Count { get; } = new BindableProperty<int>(5);
public void Init()
{
}
}
}
// 监听BindableProperty数值变化
using UnityEngine;
using UnityEngine.UI;
namespace BlueVersion2
{
public class Test : MonoBehaviour, IController
{
public Text TestText;
private void Start()
{
this.GetSystem<ITestSystem>().Count.Register((newValue) =>
{
TestText.text = newValue.ToString();
});
}
}
}
Command执行增删改操作,Query执行查操作
Query需要继承 IQuery接口
// 定义Query
using System.Threading.Tasks;
namespace BlueVersion3
{
public class TestQuery1 : IQuery<int>
{
public int DoQuery()
{
return this.GetModel<ITestModel>().TestQuery;
}
}
public class TestQueryAsync1 : IQuery<Task<int>>
{
public async Task<int> DoQuery()
{
await Task.Delay(2000);
return 111;
}
}
public class TestQueryAsync2 : IQuery<string>
{
public string DoQuery()
{
return "TestQuery2";
}
}
}
// 同步查询示例
int testQuery1 = this.SendQuery(new TestQuery1()); //使用示例1
int testQuery2 = this.SendQuery<TestQuery1, int>();//使用示例2
Debug.Log("TestQuery1:"+testQuery1);
Debug.Log("TestQuery2:"+testQuery2);
// 异步查询示例
//使用示例1
IQueryResult<Task<int>> task = this.SendQueryAsync<Task<int>>(new TestQueryAsync1());
task.OnQuerySucceed((value) =>
{
Debug.Log("NewValue:" + value.Result);
});
task.OnQueryFailed(() =>
{
Debug.Log("失败:");
});
//使用示例2
this.SendQueryAsync<string>(new TestQueryAsync2()).OnQuerySucceed((value) =>
{
Debug.Log("TestQueryAsync2:"+value);
});
工具层---需要继承 IUtility接口
获取跟Model、System类似
// 定义Utility
namespace BlueVersion3
{
public class TestUtility : IUtility
{
}
}