(转载)http://www.cnblogs.com/yjmyzz/archive/2010/08/01/1789769.html

上一篇 puremvc框架之Command 里,已经学习了如何利用Command来解耦View层与业务逻辑的依赖,但是仍然有二个问题:

1、ButtonMediator中发送消息时,仍然采用硬编码的方式,将消息内容写死在代码中:

1
2
3
private function btnClick(e:MouseEvent):void{
            this.sendNotification(AppFacade.CHANGE_TEXT,"Hello PureMVC !");
        }      

这显然不是一个好的设计,不够灵活

2、我们一直在说puremvc是一个mvc框架,至今为止 controller(即Command)、view(即Mediator)都已经出现过了,但是model层还是不见踪影,puremvc中的model层在哪里?

在asp.net mvc中,model层通常是定义数据实体的部分,可以选用的技术有很多,比如linq to sql,linq to entity ,nhibernate之类,这个概念在puremvc中仍然是相通的,只不过换了个名字,我们称之为Proxy!

先来定义一个AppProxy类吧(放到mvc.model包中),代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package mvc.model
{
    import org.puremvc.as3.interfaces.IProxy;
    import org.puremvc.as3.patterns.proxy.Proxy;
    import flash.events.IOErrorEvent;
    import flash.events.Event;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
 
 
    public class AppProxy extends Proxy implements IProxy
    {
        public static const NAME:String="AppProxy"; //名称常量
 
        public function AppProxy(proxyName:String=null, data:Object=null)
        {
            super(NAME, data);//将本proxy的名称常量传入其中
            var _loader:URLLoader=new URLLoader;
            _loader.addEventListener(Event.COMPLETE, onComplete);
            _loader.addEventListener(IOErrorEvent.IO_ERROR, onError);
            _loader.load(new URLRequest("data.xml"));
        }
 
        private function onComplete(e:Event):void
        {
 
            var _xml:XML=XML(e.target.data);
            setData(_xml); //将xml内容保存进data
        }
 
        private function onError(e:IOErrorEvent):void
        {
            trace("数据获取失败!");
        }
    }
}

这里,我们用xml做为数据源来提供数据,data.xml放到根目录下,内容如下:

1
2
3
4
<?xml version="1.0" encoding="UTF-8"?>
<message>
        <msg>Hello World!</msg>
</message>

ok,这一步做好后,老问题又来了:如何让它跟puremvc环境中的facade实例挂上勾?类似上一篇的处理,还是放到AppCommand中来处理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package mvc.controller
{
    import mvc.AppFacade;
    import mvc.model.AppProxy;
    import mvc.view.ButtonMediator;
    import mvc.view.TextMediator;
     
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.command.SimpleCommand;
 
    public class AppCommand extends SimpleCommand
    {
        public function AppCommand()
        {
            super();
        }
 
        public override function execute(inote:INotification):void
        {
            var _main:main=inote.getBody() as main;
             
            //注册proxy
            facade.registerProxy(new AppProxy());
             
            facade.registerMediator(new TextMediator(_main.txtResult));
            facade.registerMediator(new ButtonMediator(_main.btnSend));        
             
            facade.registerCommand(AppFacade.CHANGE_TEXT,ChangeTextCommand);
        }
    }
}

注意加注释的部分facade.registerProxy(new AppProxy());这样就ok了,这一步执行后,puremvc环境中就已经有data.xml的数据了

现在就可以把原来ButtonMediator中硬编码的部分去掉了,改成下面这样:

1
2
3
4
private function btnClick(e:MouseEvent):void{
        //this.sendNotification(AppFacade.CHANGE_TEXT,"Hello PureMVC !");          
        sendNotification(AppFacade.CHANGE_TEXT);
    }

即:view层只发送消息(类型),通知puvrmvc环境--“CHANGE_TEXT消息我已经发出去了!”,至于数据在哪,谁去处理,关我P事!

OK,有人发了消息,自动就要有人处理,接下来折腾ChangeTextCommand.as

1
2
3
4
5
6
7
8
public override function execute(notification:INotification):void{
             
            var _proxy:AppProxy=facade.retrieveProxy(AppProxy.NAME) as AppProxy;           
            var _xml:XML=XML(_proxy.getData());        
            trace(_xml);           
            (facade.retrieveMediator(TextMediator.NAME) as TextMediator).txtInstance.text = _xml.msg;          
            //(facade.retrieveMediator(TextMediator.NAME) as TextMediator).txtInstance.text = notification.getBody() as String;
        }

这里,我们把原来的方法注释掉了,改成用Proxy的getData获取刚才data.xml中的数据,然后该数据赋值为TextMediator相关联的文本框.

至此,M(proxy)-V(mediator)-C(command)全都登场了,相互之间也实现了完全解耦!

最后再从头回顾一下主要的处理细节:

1、man.mxml中通过 AppFacade.getInstance().startup(this) 启动puvemvc环境

2、而AppFacade又通过this.registerCommand(START_UP, AppCommand) 注册AppCommand

3、AppCommand中又通过

1
2
3
4
facade.registerProxy(new AppProxy());          
facade.registerMediator(new TextMediator(_main.txtResult));
facade.registerMediator(new ButtonMediator(_main.btnSend));    
facade.registerCommand(AppFacade.CHANGE_TEXT,ChangeTextCommand);

把mediator、proxy以及消息CHANGE_TEXT相关的ChangeTextCommand给扯进来

4、然后ButtonMediator中又通过sendNotification(AppFacade.CHANGE_TEXT)来发送自己感兴趣的消息

5、最后CHANGE_TEXT消息被与之关联的ChangeTextCommand得到,并在execute方法中处理以更新UI界面。

源文件下载:http://cid-2959920b8267aaca.office.live.com/self.aspx/flex/PureMVC^_Proxy.fxp (用FB4导入即可)

(转载)puremvc框架之proxy的更多相关文章

  1. PureMVC 框架总结收录

    PureMVC框架的目标很明确,就是把程序分为低耦合的三层:Model.View和Controller. 通过使用PureMVC后,我们的代码将集中分为以下几个部分:Façade.Command.Me ...

  2. Unity编程笔录--ulua+PureMVC框架简单热更新使用

    ulua+PureMVC框架简单热更新使用 前言: 1:作者官网论坛 首先介绍的是这个框架是一位大牛  骏擎[CP]  jarjin   写的,据说原本是"非常多人不知道怎么使用Ulua,所 ...

  3. Unity3d中PureMVC框架的搭建及使用资料

    1.下载PureMVC框架 https://github.com/PureMVC/puremvc-csharp-multicore-framework https://github.com/PureM ...

  4. Unity3d + PureMVC框架搭建

    0.流程:LoginView-SendNotification()---->LoginCommand--Execute()--->调用proxy中的函数操作模型数据--LoginProxy ...

  5. 转载--thinkphp框架的路径问题 - 总结

    转自:http://blog.sina.com.cn/s/blog_827ddd950100ulyv.html TP中有不少路径的便捷使用方法,比如模板中使用的__URL__,__ACTION__等, ...

  6. 转载ORM--EF框架

    EF4.1有三种方式来进行数据操作及持久化.分别是Database-First,Model-First,Code-first: 1.Database First是基于已存在的数据库,利用某些工具(如V ...

  7. [转载]SSH框架搭建详细图文教程

    http://www.cnblogs.com/hoobey/p/5512924.html

  8. [转载] Spring框架——AOP前置、后置、环绕、异常通知

    通知类型: 步骤: 1. 定义接口 2. 编写对象(被代理对象=目标对象) 3. 编写通知(前置通知目标方法调用前调用) 4. 在beans.xml文件配置 4.1 配置 被代理对象=目标对象 4.2 ...

  9. openwrt<转载--openwrt框架分析 >

    这次讲讲openwrt的结构. 1. 代码上来看有几个重要目录package, target, build_root, bin, dl.... ---build_dir/host目录是建立工具链时的临 ...

随机推荐

  1. iOS afnetworking最新版报错 没有AFHTTPRequestOperationManager类了

    今天开了一个小项目   用的是pod   然后  安装好 Afnetworking之后   发现 AFHTTPRequestOperationManager  这个类没有了  ,百度之后  发现 原来 ...

  2. OC的内存管理机制

    总的来说OC有三种内存管理机制,下面将分别对这三种机制做简要的概述. 1.手动引用计数(Mannul Reference Counting-MRC) mannul:用手的,手工的. 引用计数:reta ...

  3. bootstrap实现手风琴功能(树形列表)

    首先把架包拷进项目,然后在页面中引进css,js <script src="js/jquery/jquery-2.1.1.min.js"></script> ...

  4. (转)UIColor,CGColor,CIColor三者的区别和联系

    最近看了看CoreGraphics的东西,看到关于CGColor的东西,于是就想着顺便看看UIColor,CIColor,弄清楚它们之间的区别和联系.下面我们分别看看它们三个的概念: 一.UIColo ...

  5. Linux下Docker安装

    1 在 CentOS 6.4 上安装 docker   docker当前官方只支持Ubuntu,所以在 CentOS 安装Docker比较麻烦(Issue #172).   docker官方文档说要求 ...

  6. openwrt opkg update wget returned 4 wget returned 1

    最近在正捣鼓mt7620芯片的路由器,刷入openwrt Pandora系统以后想装wifidog实现web认证. 我用我自己的一个水星的路由器PPPOE拨号,通过水星的lan口连接网线到我openw ...

  7. linux 信号处理

    查看信号 kill -l 信号实际就是一个进程发送给另一个进程的消息

  8. express cookie-session解惑

    express的中间件基于connect模块而来,所以相关文档可以直接参考 http://www.senchalabs.org/connect/ 使用cookie-session中间件过程中,比较困惑 ...

  9. PHP实现斐波那契数列非递归方法

    斐波那契数列,又称黄金分割数列,指的是这样一个数列:0.1.1.2.3.5.8.13.21.……在数学上,斐波纳契数列以如下被以递归的方法定义:F0=0,F1=1,Fn=F(n-1)+F(n-2)(n ...

  10. ECMAScript 5正式发布

    这周ECMAScript 5也即众所周知的JavaScript正式发布了(pdf),在给基本库带来更新的同时,还引入了更加严格的运行时模型,来帮助定位并移除通常的代码错误. 而早期对于ECMAScri ...