(转载)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毛玻璃效果

    方法一:支持所有ios系统版本: - (void)setupBlurView { UIImageView *darkView = [[UIImageView alloc] init]; darkVie ...

  2. angularjs开发遇到的坑

    1.用ng-model绑定的输入input标签不能序列化$("form").serialize();要在value赋值才行. 2.disabled 是不能被序列化提交的(这不属于n ...

  3. 哥德巴赫猜想证明(C语言实现50以内的正偶数证明)

    <一>哥德巴赫猜想内容: 一个充分大的偶数(大于或等于6)可以分解为两个素数之和. <二>实现要点: 要点: 判断素数(质数):除了1和本身没有其他约数. 最小的质数:2 判断 ...

  4. STL:remove和erase区别

    C++ STL中的remove和erase函数曾经让我迷惑,同样都是删除,两者有什么区别呢? vector中的remove的作用是将等于value的元素放到vector的尾部,但并不减少vector的 ...

  5. IOS 学习笔记 2015-04-03 OC-API-文件读写

    // // WPFileHelper.m // OC-API-文件操作 // // Created by wangtouwang on 15/4/3. // Copyright (c) 2015年 w ...

  6. 微软AJAX解决方案

    -------- 微软AJAX解决方案 (*) --------ASP.Net中内置的简化AJAX开发的控件UpdatePanel 放入ScriptManager,将要实现AJAX效果的控件放到Upd ...

  7. MongoDB笔记(五)深入学习

    系列一:http://www.cnblogs.com/huangxincheng/category/355399.html系列二:http://www.cnblogs.com/lipan/catego ...

  8. Linux的IO调度

    Linux的IO调度 IO调度发生在Linux内核的IO调度层.这个层次是针对Linux的整体IO层次体系来说的.从read()或者write()系统调用的角度来说,Linux整体IO体系可以分为七层 ...

  9. Oracle 10g 下载地址

    Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise/Standard Edition for Microsoft Windows (32-bit ...

  10. xcode 自动添加注释,生成文档

    一.自动生成注释代码        添加一个快捷键,生成 注释代码        ThisService 下载连接:http://wafflesoftware.net/thisservice/     ...