Unity - 对PureMVC的理解和应用
#Unity - PureMVC的理解和应用
PureMVC顾名思义,比MVC更纯净的MVC架构,相比与MVC它耦合性更低、代码重用性更高,当然缺点也比较明显:事件的传递都要经过拆箱装箱、事件的执行都需要用反射性能不高、代码冗余。但不失为新手学习的好框架,也可以直接放到项目中应用。
##PureMVC核心
Facade
对应设计模式的外观模式:http://www.runoob.com/design-pattern/facade-pattern.html。 它是PureMVC的外部入口,用于初始化Model、View、Controller,可以操作任何的Proxy、Mediator、Command。
Model与Proxy
对应设计模式的代理模式:http://www.runoob.com/design-pattern/proxy-pattern.html。 Proxy接受Command、Mediator的通知,对数据(图中的Data Objects)进行处理,并把结果保存到数据中。
View与Mediator
对应设计模式的中介者模式:http://www.runoob.com/design-pattern/mediator-pattern.html。 对UI的操作由Mediator来管理,包括添加事件监听,发送或接受Notification,改变UI状态等。图中的View Components在Unity中对应的就是封装好的UI.Image、UI.Text等组件。Mediator可以直接对Proxy进行操作,并接受Command的通知。
Contoller与Command
对应设计模式的命令模式:http://www.runoob.com/design-pattern/command-pattern.html。 Controller已经在Facade中被隐式创建好,因此只需要创建对应的Command并用Facade注册即可。Command控制Proxy中的数据该如何处理,将结果通知Proxy进行存储,命令完成后通知Mediator显示结果。
Notifier与Observer
各个模块的消息传递都是用观察者模式:http://www.runoob.com/design-pattern/observer-pattern.html 。Proxy、Mediator、Command都是Notifier,他们都会发消息,对这些消息感兴趣的都是Observer,比如Command会观察View的按钮、View观察Command的计算结果,初始时它们都会new一个Observer把函数名等保存到View中,执行时通过反射执行函数。
简单应用
一个简单的相加计算。
//MyEvent.cs所有事件
namespace PureMVCTest
{public class MyEvent{// 计算事件public const string CALCULATE = "Calculate";// 输入错误事件public const string INPUTERROR = "InputError";// 结果事件public const string RESULT = "Result";}
}
// MyProxy.cs
using PureMVC.Patterns;namespace PureMVCTest
{// Model数据结构public struct Data{public float input1;public float input2;public float result;}// Model的代理public class MyProxy : Proxy{public new const string NAME = "MyProxy";// 数据protected Data m_Data;public float input1 { get { return m_Data.input1; } }public float input2 { get { return m_Data.input2; } }public MyProxy() : base(NAME){}// 对Model数据的操作public void SetInput(float input1, float input2){m_Data.input1 = input1;m_Data.input2 = input2;}public void SetResult(float result){m_Data.result = result;}}
}
// MyMediator.cs
using PureMVC.Patterns;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;namespace PureMVCTest
{public class MyMediator : Mediator{public new const string NAME = "MyMediator";private InputField input1;private InputField input2;private Text result;private Button calculateButton;public MyMediator(Transform root){input1 = root.Find("Input1").GetComponent<InputField>();input2 = root.Find("Input2").GetComponent<InputField>();result = root.Find("Result").GetComponentInChildren<Text>();calculateButton = root.Find("Calculate").GetComponent<Button>();calculateButton.onClick.AddListener(OnButtonClick);}// UI的点击事件private void OnButtonClick(){float f1, f2;if (!System.Single.TryParse(input1.text, out f1)){HandleSelfNotification(MyEvent.INPUTERROR);return;}if (!System.Single.TryParse(input2.text, out f2)){HandleSelfNotification(MyEvent.INPUTERROR);return;}MyProxy proxy = (MyProxy)Facade.RetrieveProxy(MyProxy.NAME);proxy.SetInput(f1, f2);SendNotification(MyEvent.CALCULATE);}// 感兴趣的Eventpublic override IList<string> ListNotificationInterests(){IList<string> list = new List<string>();list.Add(MyEvent.RESULT);return list;}// 处理感兴趣的Eventpublic override void HandleNotification(PureMVC.Interfaces.INotification notification){switch (notification.Name){case MyEvent.RESULT:float ret = (float)notification.Body;result.text = ret.ToString();input1.text = string.Empty;input2.text = string.Empty;break;default:break;}}public void HandleSelfNotification(string notificationName){switch (notificationName){case MyEvent.INPUTERROR:result.text = "输入错误!";input1.text = string.Empty;input2.text = string.Empty;break;default:break;}}}
}
// MyCommand.cs
using PureMVC.Interfaces;
using PureMVC.Patterns;namespace PureMVCTest
{public class AddCommand : Notifier, ICommand{// 执行命令public void Execute(INotification notification){MyProxy proxy = (MyProxy)Facade.RetrieveProxy(MyProxy.NAME);float result = proxy.input1 + proxy.input2;proxy.SetResult(result);SendNotification(MyEvent.RESULT, result);}}
}
// MyTest.cs挂载到Canvas上
using UnityEngine;namespace PureMVCTest
{public class MyTest : MonoBehaviour{// 整个PureMVC的起点void Start(){PureMVC.Interfaces.IFacade facade = PureMVC.Patterns.Facade.Instance;// 初始化所有的Proxy、Mediator、Commandfacade.RegisterProxy(new MyProxy());facade.RegisterMediator(new MyMediator(transform));facade.RegisterCommand(MyEvent.CALCULATE, typeof(AddCommand));}}
}
执行顺序
UI OnButtonCilck事件 -> Mediator直接操作Proxy储存输入 -> Mediator发消息给Command -> Command执行命令,在Proxy中保存结果,并将结果发消息给Mediator -> Mediator接受消息改变显示
链接
PureMVC官网:http://puremvc.org/
PureMVC Github:https://github.com/PureMVC
参考文章:https://www.jianshu.com/p/47deaced9eb3
Unity - 对PureMVC的理解和应用相关推荐
- UNITY 画布的粗浅理解
UNITY 画布的粗浅理解 画布:当画布是screen-space overlay时,这个好理解,画布可以控制如分辨率,层次等. 但当画布是 world-space时,这个严格来说就不算是一个画布了, ...
- Unity 新手入门 如何理解协程 IEnumerator yield
Unity 新手入门 如何理解协程 IEnumerator 本文包含两个部分,前半部分是通俗解释一下Unity中的协程,后半部分讲讲C#的IEnumerator迭代器 协程是什么,能干什么? 为了能通 ...
- 关于Puremvc的理解
PureMVC框架的目标很明确,即把程序分为低耦合的三层:Model.View和Controller.它们合称为PureMVC框架的核心,由Facade统一管理.关于它的核心层,我们不需要管太多,只需 ...
- 在Unity使用PureMVC
1.动机 由于在topameng的cstolua框架中使用到了PureMVC框架,本人初学热更新时便感到了一头雾水,因此又特地的了解学习了PureMVC框架. 2.导入PureMVC框架 首先要说一些 ...
- 【unity】 PureMvc 入门尝试小案例
目录 目录 1:写pureMvc第一步需要做什么? 2:业务执行顺序是什么? 3:主要代码 3.1:数据部分(Proxy) 3.1.1:原始数据面板中text显示的value 3.1.2:proxy类 ...
- Unity 内存优化之理解托管堆和本机堆
https://docs.unity3d.com/Manual/BestPracticeUnderstandingPerformanceInUnity4-1.html https://www.dazh ...
- 新手对于Unity协程的理解
首先从monoBehavior的生命周期来谈谈协程 我们可以从官网上得到一个图. 从图中,我们可以猜测协程是实现在Update之后且在LateUpdate之前的,并且yield return之后的内容 ...
- Unity UI框架详细理解--场景管理
SceneState类:存放场景状态 OnEnter方法:负责实行场景进入时候的方法 Onexit方法:负责实行场景退出时候的方法 脚本存放的位置: ...
- PureMVC游戏框架解析 理解其中包含的设计模式
作者:吴秦 出处:http://www.cnblogs.com/skynet/ 本文基于署名 2.5 中国大陆许可协议发布,欢迎转载,演绎或用于商业目的,但是必须保留本文的署名吴秦(包含链接). 参考 ...
最新文章
- CentOS全局配置JAVA环境变量,实现多个tomcat共用环境变量,不用再一个个tomcat配置
- 4、Python —— 函数
- 企业网络推广—企业网络推广专员一定要避免这些不靠谱的优化方式
- 小学生python-用Python实现小学生四则运算
- Gloomy对Windows内核的分析
- tensorflow随笔-tf.while_loop
- Cpp / 右值、纯右值、将亡值
- SecurityUtil
- 使用Visio Viewer载入数据库中的Visio图
- 冲上热搜!快手宣布取消大小周
- UNIX环境高级编程——线程同步之条件变量以及属性
- python编程可以自学么-怎么能学习好python编程?有自学的方法吗?
- Nature:人类癌细胞系转移图谱
- 《数字图像处理 第三版》(冈萨雷斯)——第十一章 表示和描述
- 【深度学习框架-torch】torch.norm函数详解用法
- 知网研学不同电脑端同步无效问题
- 计算机组成原理速成课程【速成】
- 马云谈加班、996看法
- 计算机应用oas,办公自动化系统(OAS)
- 前缀、中缀、后缀表达式
热门文章
- [想要访问若依后台]若依框架报错401请求访问:error认证失败,无法访问系统资源
- PostgreSQL数据库集簇
- 2022爱分析· 中国云数据平台市场厂商评估报告:数新网络
- unity log4net
- PaddlePaddle/PaddleX本地离线安装(分别以C++和Python为例)
- OpenGL坐标变换及其数学原理,两种摄像机交互模型(附源程序)
- 推荐引擎算法 - 猜你喜欢的东西
- 华为云服务-应用部署4-后端组件部署
- 小心!显卡BIOS刷新工具Nvflash变杀手
- python--判断结构(一元二次方程)