Puremvc
Puremvc
PureMVC健壮、易扩展、易维护
Many so-called Model-View-Controller frameworks today seem to include everything but the kitchen sink. We decided that basic separation of responsibilities according to the MVC meta-pattern could be expressed with a handful of actors. We gave them a built-in means to communicate with each other which reinforces the classic MVC collaboration patterns, and did so with the simplest possible language features. That's why it's been ported to so many languages from the original ActionScript. It just works, with no "magic happens here" bits.
PureMVC优缺点:
- 1.利用中介者,代理者,命令实现解耦,使得Model、View、Controller之间耦合性降低,提升了部分代码的重用
- 2.View界面可以实现重用
- 3.Model数据可以实现重用
- 3.代码冗余量大,对于简单的功能都得创建View、Mediator、Command、Facade,Proxy,Model脚本
- 4.操作过程比较繁琐的流程,Mediator中的代码会显得流程较为复杂难懂,除非你很熟悉PureMVC执行原理
PureMVC特点:
- 1.通知的传递都要经过装箱和拆箱的操作
- 2.命令/通知是以观察者模式实现,命令/通知在观察者中利用反射获取方法并执行
- 3.没有Service(可按照MVC的构造,自行添加与网络通讯的这个模块)
- 4.数据通过通知传递,SendNotification只有一个object类型参数,会感觉数据传输受限,可以将数据组合成一个类型/结构传递,或者是为Notification再拓展一个参数。
一、结构及示例

1、PureMVC结构
- PureMVC把程序分为低耦合的三层:Model、View和Controller。
- ureMVC实现的经典MVC元设计模式中,这三部分由三个单例模式类管理,分别是Model、View和Controller。三者合称为核心层或核心角色。
-PureMVC中还有另外一个单例模式类——Facade,Facade提供了与核心层通信的唯一接口,以简化开发复杂度。
2、层级关系

PureMVC使用了观察者模式,所以各层之间能以一种松耦合的方式通信,并且与平台无关。
2.1、Model
数据层
- public class MyData
- {
- public int DataValue { get; set; }
- }
2.2、Proxy
Proxy负责操作具体的数据模型,这样保住了Model层的可移植性,Model只需维持自己的数据结构就好。
- using PureMVC.Patterns;
- /// <summary>
- /// 通过proxy代理来进行对数据model的操作
- /// </summary>
- public class MydataProxy : Proxy
- {
- public const string proxname = "MyData01";
- public MyData MyData;
- public MydataProxy() : base(proxname)
- {
- MyData = new MyData();
- }
- public void AddValue()
- {
- MyData.DataValue++;
- //Facade 和Proxy只能发送Notification,Mediators既可以发送也可以接收Notification,Notification被映射到 Command,
- //同时Command也可以发送Notification。这是一种“发布/订阅”机制,所有的观察者都可以收到相同的通知。
- //例如多个书刊订阅者可以订阅同一份杂志,当杂志有新刊出版时,所有的订阅者都会被通知。
- SendNotification("msg_add",MyData);
- }
- public void SubValue()
- {
- MyData.DataValue--;
- SendNotification("msg_add", MyData);
- }
- }
2.3、Mediator
Mediator中介由,Mediator对象来操作具体的视图组件,包括:添加事件监听器,发送或接收Notification ,直接改变视图组件的状态。
这样做实现了把视图和控制它的逻辑分离开来。
- using System.Collections.Generic;
- using PureMVC.Interfaces;
- using PureMVC.Patterns;
- using UnityEngine;
- using UnityEngine.UI;
- public class MyMediator : Mediator
- {
- public const string MediatorName = "MyMeditor";
- public Text textNum;
- public Button AddButton;
- public Button SubButton;
- public MyMediator(GameObject root):base(MediatorName)
- {
- //持有对视图中对象的引用
- textNum = root.transform.Find("TextValue").GetComponent<Text>();
- AddButton= root.transform.Find("BtnAdd").GetComponent<Button>();
- SubButton= root.transform.Find("BtnSub").GetComponent<Button>();
- // 视图中的操作
- AddButton.onClick.AddListener(Addbtn);
- SubButton.onClick.AddListener(Subbtn);
- }
- /// <summary>
- /// 操作视图
- /// </summary>
- /// <param name="myData">数据对象</param>
- private void DisplayFun(MyData myData)
- {
- textNum.text = myData.DataValue.ToString();
- }
- /// <summary>
- /// 与command层通信
- /// </summary>
- private void Subbtn()
- {
- SendNotification("cmd_sub");
- }
- /// <summary>
- /// 与command层通信
- /// </summary>
- private void Addbtn()
- {
- SendNotification("cmd_add");
- }
- /// <summary>
- ///定义自己需要关心的消息
- /// </summary>
- /// <returns></returns>
- public override IList<string> ListNotificationInterests()
- {
- List<string> list=new List<string>()
- {
- "msg_add","msg_sub"
- };
- return list;
- }
- /// <summary>
- /// 处理消息
- /// </summary>
- /// <param name="notification"></param>
- public override void HandleNotification(INotification notification)
- {
- switch (notification.Name)
- {
- case "msg_add":
- case "msg_sub":
- DisplayFun(notification.Body as MyData);
- break;
- default:
- break;
- }
- }
- }
2.4、Command
Command命令层,Command可以获取Proxy对象并与之交互,发送Notification,执行其他的Command。经常用于复杂的或系统范围的操作,如应用程序的“启动”和“关闭”。应用程序的业务逻辑应该在这里实现。
- using PureMVC.Interfaces;
- using PureMVC.Patterns;
- public class MyCommandAdd : SimpleCommand
- {
- public override void Execute(INotification notification)
- {
- //直接调度Proxy处理具体逻辑
- MydataProxy mydataProxy = Facade.RetrieveProxy("MyData01") as MydataProxy;
- mydataProxy.AddValue();
- }
- }
- public class MyCommandSub : SimpleCommand
- {
- public override void Execute(INotification notification)
- {
- MydataProxy mydataProxy = Facade.RetrieveProxy("MyData01") as MydataProxy;
- mydataProxy.SubValue();
- }
- }
2.5、MyFacade
Facade类应用单例模式,它负责初始化核心层(Model,View和Controller),并能访问它们的Public方法。
这样,在实际的应用中,你只需继承Facade类创建一个具体的Facade类就可以实现整个MVC模式,并不需要在代码中导入编写Model,View和Controller类。
Proxy、Mediator和Command就可以通过创建的Façade类来相互访问通信。
一般地,实际的应用程序都有一个Facade子类,这个Facade类对象负责初始化Controller(控制器),建立Command与Notification名之间的映射,并执行一个Command注册所有的Model和View。
- using PureMVC.Patterns;
- using UnityEngine;
- public class MyFacade : Facade
- {
- public MyFacade(GameObject root) : base()
- {
- //注册命令,实现消息与命令之间的绑定
- RegisterCommand("cmd_add",typeof(MyCommandAdd));
- RegisterCommand("cmd_sub",typeof(MyCommandSub));
- RegisterMediator(new MyMediator(root));
- RegisterProxy(new MydataProxy());
- }
- }
2.6 初始化Facade
注意:除了顶层的Application,其他视图组件都不用(不应该)和Façade交互。
顶层的Application构建视图结构、初始化Façade,然后“启动”整个PureMVC机制。
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class GameManager : MonoBehaviour
- {
- public GameObject root;
- // Start is called before the first frame update
- void Start()
- {
- //初始化MyFacade启动PureMVC
- MyFacade myFacade = new MyFacade(root);
- }
- }
它山之石可以攻玉
Puremvc的更多相关文章
- Lua pureMVC
分享一个lua语言版本的pureMVC. 这个是一个根据AS3(ActionScript 3) pureMVC而转换过来的lua pureMVC.所有的接口完全跟AS3版本一致,本来是想用在项目之中的 ...
- 最简puremvc
工程如下,看来sendNotification是像comand和mediator发消息的 package { import flash.display.Sprite; import flash.eve ...
- pureMVC学习之一
//1var MainWindow: TMainWindow;begin Application.Initialize; Application.MainFormOnTaskbar := Tru ...
- 【AS3 Coder】任务七:初涉PureMVC——天气预报功能实现
转自:http://www.iamsevent.com/post/36.html AS3 Coder]任务七:初涉PureMVC——天气预报功能实现 使用框架:AS3任务描述:了解PureMVC框架使 ...
- PureMVC(JS版)源码解析:总结
PureMVC源码中设计到的11个类已经全部解析完了,回首想想,花了一周的时间做的这点事情还是挺值得的,自己的文字组织表达能力和对pureMVC的理解也在写博客的过程中得到了些提升.我也是第一次写系列 ...
- (转载)puremvc框架之proxy
(转载)http://www.cnblogs.com/yjmyzz/archive/2010/08/01/1789769.html 上一篇 puremvc框架之Command 里,已经学习了如何利用C ...
- 基于HTML5的SLG游戏开发( 三):认识PureMVC
在游戏开发中,对于一般网络游戏,由于需要多人协同开发,每个人负责不同的模块开发,为了减少耦合,需要用来一些MVC框架,减少模块之间的耦合.我们现在使用的mvc框架是pureMVC.pureMVC的官网 ...
- PureMVC(JS版)源码解析(一):观察者模式解析
假设一种情景,在程序开发中,我们需要在某些数据变化时,其他的类做出相应,例如在游戏中,升级一件装备,会触发玩家金币数量改变,背包数据改变和冷却队列数据改变等等.我们不可能设置setInte ...
- PureMVC(JS版)源码解析(二):Notification类
上篇博客,我们已经就PureMVC的设计模式进行的分析,这篇博文主要分析Notification(消息)类的实现. 通过Notification的构造函数可以看出,PureMVC中的Notificat ...
- PureMVC(JS版)源码解析(三):Observer类
上一篇博客中,我们讲到了Notification类(消息类),Notification(消息)是连接观察者(observer)和通知者(notifier)之间的桥梁.这一篇博客,主要是在代 ...
随机推荐
- 关于Git和Svn的区别
关于Git 和 Svn 的选用,详细列出区别 Git 是分布式的,而 Svn 不是分布的; Git 把内容按元数据方式存储,而 SVN 是按文件; Git 没有一个全局版本号,而 SVN 有:目前为止 ...
- Java数字转中文数字——支持:Integer、BigDecimal
1.效果 public static void main(String[] args) { System.out.println(int2chineseNum(3456)); System.out.p ...
- 2023 Stack Overflow 调研
一.Programming, scripting, and markup languages 二.Databases 三.Web frameworks and technologies 四.Other ...
- PostgreSQL世界上最先进的开源关系型数据库
PostgreSQL 的 Slogan 是 "世界上最先进的开源关系型数据库". PostgreSQL是一个功能非常强大.源代码开放的对象关系数据库系统(ORDBMS),在灵活的B ...
- 【OpenVINO™】在 C# 中使用OpenVINO™ 部署PP-YOLOE实现物体检测
前言 OpenVINO C# API 是一个 OpenVINO 的 .Net wrapper,应用最新的 OpenVINO 库开发,通过 OpenVINO C API 实现 .Net 对 OpenV ...
- CSS布局概念与技术教程
以下是一份CSS布局学习大纲,它涵盖了基本到高级的CSS布局概念和技术 引言 欢迎来到CSS教程!如果你已经掌握了HTML的基础知识,那么你即将进入一个全新的世界,通过学习CSS(Cascading ...
- Windows 10 开启秒钟显示
开始搜索运行 注册表管理器 regedit 定位路径到 HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Explorer\Adv ...
- 使用 JS 实现在浏览器控制台打印图片 console.image()
在前端开发过程中,调试的时候,我门会使用 console.log 等方式查看数据.但对于图片来说,仅靠展示的数据与结构,是无法想象出图片最终呈现的样子的. 虽然我们可以把图片数据通过 img 标签展示 ...
- 定了!AIRIOT新品发布会,6月6日北京见。
随着物联网.大数据.AI技术的成熟和演进,智能物联网技术正在加速.深入渗透至各行业应用. AIRIOT物联网平台作为赋能数字经济发展和产业转型的数字基座,由航天科技控股集团股份有限公司(股票代码:00 ...
- kubernetes 之Health Check 健康检查
默认的健康检查 这里Pod的restartPolicy设置为OnFailure,默认为Always. [machangwei@mcwk8s-master ~]$ cat mcwHealthcheck. ...