单核版本下载地址:
源:https://en.wikipedia.org/wiki/Singleton_pattern
自传蓝奏云:https://wwz.lanzoum.com/iv3KY07bzvje
PureMVC框架单核版本使用逻辑
首先编写PureNotification类:
这是通知名称类,使用常量string保存通知名称
Model层:
1. 编写数据结构类,纯class->PlayerDataObj
2. 编写继承Proxy的代理类,其中Proxy需要编写一个必选参数“名称",一个可选参数”数据变量“的构造方法
例:public PlayerProxy(string name,object data=null);
Proxy代理使用Data属性存储数据,使用Facade.RegisterProxy注册代理,可使用Facade.Retri…通过名字获取数据。
Proxy固定写法:
public class PlayerProxy : Proxy
{
public new const string NAME = "PlayerProxy";
public PlayerProxy() : base(NAME)
{
XXX data=new XXX();
....
Data=data;
}
...其他数据业务逻辑
例如 PlayerLevelUp()数据升级逻辑
}
View层:
1. 为每个面板都编写View类,继承MonoBehaviour,用于声明UI组件以及编写更新UI信息方法UpdateInfo(XXX 数据信息类)
2. 为每个View类编写Mediator类,需要继承Mediator
Mediator固定写法:
public class MainView_Mediator : Mediator
{
public static new string NAME = "Mediator名称,例如MainView_Mediator";
public MainView_Mediator() : base(NAME)//构造函数,不需要写业务逻辑,调用父类的单参数构造函数即可
{
}
public override string[] ListNotificationInterests()//重写该方法,用于告诉脚本该类需要监听哪些通知,通知名存在PureNotification中
{
return new string[] {
PureNotification.UPDATE_INFO
};
}
public override void HandleNotification(INotification notification)//重写触发方法
{
switch (notification.Name)//通过通知的名称判断需要做什么逻辑,前提是在ListNotificationInterests中添加好了通知名称
{
case PureNotification.UPDATE_INFO:
if(ViewComponent!=null)
((XXXView)ViewComponent).UpdateView((XXX)notification.Body);
break;
}
}
public void SetView(XXXView view类)
{
ViewComponent=view类;//为Mediator类设置View脚本,这个是必要步骤
}
}
Controller层:
这里编写的是业务逻辑,也就是发送通知时触发的方法,所有类都继承SimpleCommand类,并重写Execute方法
public class StartUp_Command : SimpleCommand
{
public override void Execute(INotification notification)
{
Debug.Log(notification.Name + "Excute");
}
}
Facade管理类脚本:
最重要的就是GameFacade脚本(名字根据自己的项目设定,最好加一个Facade后缀,以表示这是Facade脚本),该脚本继承Facade类,并且需要编写为单例脚本,并重写InitializeFacade方法,里面编写使用RegisterCommand注册Command命令的逻辑
示例脚本:
public class GameFacade : Facade
{
public static GameFacade Instance
{
get
{
if(instance==null)
{
instance = new GameFacade();
}
return instance as GameFacade;
}
}
protected override void InitializeFacade()
{
base.InitializeFacade();
RegisterCommand(PureNotification.START_UP,()=> {
return new StartUp_Command();
});
RegisterCommand(PureNotification.SHOW_PANEL, () => {
return new ShowPanel_Command();
});
RegisterCommand(PureNotification.HIDE_PANEL, () =>
{
return new HidePanel_Command();
});
RegisterCommand(PureNotification.LEV_UP, () =>
{
return new LevUp_Command();
});
}
注册完毕后使用SendNotification(PureNotification.通知名);进行发起通知