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)之间的桥梁.这一篇博客,主要是在代 ...
随机推荐
- 基于 MaxCompute 的智能推荐解决方案
简介: 在互联网行业红利已过.在获客成本越来越高.在用户在线时长全网基本无增长以及信息大爆炸的情况下,如何更好的转化新用户和提升老用户粘性就变得至关重要,智能化的个性化推荐无疑是经过验证的重要手段之一 ...
- dotnet SemanticKernel 入门 将技能导入框架
在上一篇博客中和大家简单介绍了 SemanticKernel 里的技能概念,接下来咱准备将 技能 导入到 SemanticKernel 框架里面,进行一个管道式调用 本文属于 SemanticKern ...
- C语言程序设计-笔记6-数组
C语言程序设计-笔记6-数组 例7-1 输出所有大于平均值的数.输入n个整数(1 ),计算这些数的平均值,再输出所有大于平均值的数. #include<stdio.h> int main ...
- 混合开发中,app内嵌h5页面时,安卓ios遇到的一些兼容问题及解决方法
1.input[type=checkbox]在ios端样式显示异常,黑色背景或边框,安卓正常 解决: input[type=checkbox]:checked{ background-color: t ...
- Mybatis学习五($和#区别以及其他tips)
1.$和#区别 1 #是将传入的值当做字符串的形式,eg:select id,name,age from student where id =#{id},当前端把id值1,传入到后台的时候,就相当于 ...
- 自定义的基于System.Net.Http.HttpClient的WebClient,可以作为微信支付宝的发起请求时的基础请求类
个人编写的,自己用于自己的微信api的请求的实现当中,源码公开,大家可以查看反编译源码.以下是使用方法: 第一步 搜索和安装zmjtool 第二步 发起请求 1 /**引入命名空间*/ 2 using ...
- linux终端显示git分支的配置
1.查看现有配置 $ echo $PS1 2.显示git分支 打开./.bashrc文件 添加以下几行命令: git_branch() { git branch 2> /dev/null | s ...
- 如何将 Win10 企业版转换为专业版
有时候,我们需要将 Windows 10 企业版转换为专业版.这种情况可能发生在您购买了预装企业版的电脑,但实际上只需要专业版的功能.本文将介绍如何简单地将 Windows 10 企业版转换为专业版. ...
- JDK源码阅读-------自学笔记(四)带标签的break和continues
"标签"是指后面跟一个冒号的标识符,例如:"label:".对Java来说唯一用到标签的地方是在循环语句之前.而在循环之前设置标签的唯一理由是:我们希望在其中嵌 ...
- java 执行 javascript 代码
package com.ruoyi.project.front.controller; import java.math.BigDecimal; import java.util.*; import ...