链接: https://pan.baidu.com/s/1VPjIzLCOePnSL9r9kkLWWg 提取码: i9sj

01:MVC的定义

PureMVC 简单案例_数据


PureMVC 简单案例_mvc_02


PureMVC 简单案例_mvc_03


02:下载安装PureMVC

PureMVC 简单案例_mvc框架_04


1.Proxy(代理) 相当于经典MVC中的Model.它将代理Model的所有职权。负责维护和操作应用程序的数据模型。Proxy通常情况下会暴露一组公共API供Controller(command)或View(mediator)调用,以获取、修改或更新程序数据。注意Proxy只可以发送通知,不可以也不应该让它接收通知。

2. Mediator(中介) 对应MVC中的View.它接管了View的所有职权,负责数据内容的视图呈现和捕获用户输入。Mediator管理者View Component, Mediator可以监听View Component发出的Event,它通过这些事件监听捕获用户输入。Mediator可以接收或发送通知,以跟Controller(Command)或其他Mediator交流。Mediator用于Model(Proxy)对外暴露的公共API操控Model(Proxy);

3. Command(命令)。它相当于一个指挥部,负责协调调度PureMVC中的Proxy和Mediator以转发通知。Command以接收或发送通知的方式跟View(Mediator)或其他Command通信,用Model(Proxy)暴露的公共API操控Model(Proxy).

4. Façade(经纪人) 在PureMVC中,M、V、C都是被作为单例封装在Façade类中的,他们对外不可见。在使用PureMVC框架的时候,不需要去创建Model、View和Controller,只需要创建一个Façade的单例,它就会自动创建出M、V、C这三个核心模块,并且为他们建立起通信机制。我们只要调用Façade暴露出来的公共方法就可以启动整个程序,所有的消息流转都在Façade内部完成。

5. View Component 是PureMvc框架中真正的可视元素,例如一个影片剪辑或一个按钮等。它通过事件与Mediator交流。通过向外暴露的API供Mediator操作。

6. Data Object 是PureMVC框架中真正的数据,它可以是一个数值对象、一个集合或是一个数据库。它通过Proxy的构造函数传入Proxy,并被一个名为data的属性引用。

03:创建脚本

PureMVC 简单案例_数据_05

PureMVC 简单案例_mvc框架_06


PureMVC 简单案例_mvc框架_07


MyDate 模型层

/****************************************************
功能:模型类 存储原始数据
*****************************************************/

using UnityEngine;

public class MyDate
{
private int _level = 0;

public int Level
{
get
{
return _level;
}

set
{
_level = value;
}
}
}

DataProxy 模型代理类

/****************************************************
功能:模型代理类 数据操作
*****************************************************/

using PureMVC.Patterns;
using UnityEngine;

public class DataProxy : Proxy
{
//覆盖父类的字段
public new const string NAME = "DataProxy";
private MyDate _myDate = null;

public DataProxy():base(NAME)
{
_myDate=new MyDate();
}

//增加的方法
public void AddLevel(int number=1)
{
_myDate.Level += number;
//数据发送给视图层
SendNotification("MsgAddLevel",_myDate);
}


}

DataMediator 视图层

/****************************************************
功能:视图类 显示
*****************************************************/

using System.Collections.Generic;
using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;
using UnityEngine.UI;

public class DataMediator : Mediator
{
public new const string NAME = "DataMediator";

private Text text;
private Button button;

public DataMediator(GameObject root)
{
text = root.transform.Find("Text").GetComponent<Text>();
button= root.transform.Find("Button").GetComponent<Button>();
button.onClick.AddListener(() =>
{
//向控制层发送消息
//发送点击事件
SendNotification("RegCommand");
});
}

//容许接收消息
public override IList<string> ListNotificationInterests()
{
IList<string> list=new List<string>();
//接收模型层的数据
list.Add("MsgAddLevel");
return list;
}

//处理数据
public override void HandleNotification(INotification notification)
{
switch (notification.Name)
{
case "MsgAddLevel":
MyDate date=notification.Body as MyDate;
text.text = date.Level.ToString();
break;
}
}
}

DataCommand 控制层

/****************************************************
功能: 控制类
*****************************************************/

using PureMVC.Interfaces;
using PureMVC.Patterns;
using UnityEngine;

public class DataCommand : SimpleCommand
{

//执行方法
public override void Execute(INotification notification)
{
//获取代理;
DataProxy proxy=(DataProxy)Facade.RetrieveProxy(DataProxy.NAME);
proxy.AddLevel(10);
}
}

ApplicationFacade 全局管理类

/****************************************************
功能:全局管理类
*****************************************************/

using PureMVC.Patterns;
using UnityEngine;

public class ApplicationFacade : Facade
{
public ApplicationFacade(GameObject root)
{
//关联三个层
RegisterCommand("RegCommand",typeof(DataCommand));
RegisterMediator(new DataMediator(root));
RegisterProxy(new DataProxy());
}
}

挂载到Canvas上

/****************************************************
功能:开始游戏
*****************************************************/

using UnityEngine;

public class StartGame : MonoBehaviour
{

private void Start()
{
new ApplicationFacade(this.gameObject);
}

}

PureMVC 简单案例_mvc_08


PureMVC 简单案例_数据_09


PureMVC框架机制

PureMVC 简单案例_mvc_10


PureMVC 简单案例_数据_11


PureMVC 简单案例_数据_12


PureMVC 简单案例_mvc_13


PureMVC 简单案例_数据_14


PureMVC 简单案例_数据_15