存档

文章标签 ‘puremvc’

(转)介绍 PureMVC框架扩展类库:fabrication

2009年8月30日 评论已被关闭

用过PureMVC框架的开发者一定在开发过程中遇到一些尴尬,在使用该框架过程中多少有些不够方便,即使很小的功能也要编写许多模版代码来实现,没有更加简洁途径来达到目的。如果你有这些抱怨的话,就该关注Fabrication这个框架了, Fabrication是一个加速开发基于PureMVC框架的Flex/Flash应用的工具,它为PureMVC编程语法添加了许多有用的特色,fabrication的建立主要是为了支持多模块应用而且只使用多核版PureMVC来扩展实现。

Fabrication使用PureMVC的Pipes Utility来沟通模块之间的通讯,但是pipes之间的内部逻辑被改造而抽象成一种更加直观的Router方式。Fabrication中的一些重要特色列举如下:

  1. 消除application中facade的使用,即facade被内置在FlexApplication或者FlexModule中,无需在开发者扩展的应用中再次声明;
  2. 反射性的Mediator注册,mediator的注册无需再等到对应的viewComponent生成后进行,fabrication里面内置的组件解析器已经帮你做到将延迟生成的组件生成mediator;
  3. 反射性的Notification处理器,你不必在mediator中指定一个notification的数组,相反你只需简单的写一个处理函数,例如respondTo<NotificationName>,fabrication就自动为你调用这个响应函数;
  4. 自动化的多核管道处理核和消息路由处理,fabrication简化了PureMVC中对多个模块通知的过程,在模块中发送消息只需简单的使用routeNotification方法就可以了;
  5. 支持不同类型的Undo/Redo;

编者点评:可以说,fabrication大大简化了PureMVC框架使用的过程,而且更加直观和易于理解,但是这不意味着你可以轻易的使用PureMVC,如果想使用这个框架的话还需要对PureMVC的内在机制做详细了解,在此之上,在基于Fabrication做开发将如鱼得水。

来至:http://www.riameeting.com/node/276

分类: Flex 标签:

pureMVC 例子

2008年8月19日 评论已被关闭
分类: 生活杂谈 标签:

puremvc 中文帮助asdoc下载

2008年5月1日 评论已被关闭

puremvc 中文帮助asdoc下载

下载地址:http://w15.easy-share.com/1700279511.html

分类: 生活杂谈 标签: ,

puremvc 中文文档下载

2008年4月29日 评论已被关闭

今天无意看到有人翻译成中文了,真是太好了,非常感觉这位仁兄。

puremvc 中文文档(pdf格式)下载地址:
http://w15.easy-share.com/1700262403.html

分类: 生活杂谈 标签: ,

PureMVC的介绍,教程等资源

2008年2月28日 评论已被关闭

PureMVC是一个由controller、Model 、view 三个子系统构成的,由Facade,实现统一管理的一个系统架构。在设计模式中对于Facade的描述为,Facade是个对象结构模式,为子系统中的接口提供一个一致的界面,Facade模式定义了一个高层接口,这一系统使得子系统变得更易于控制。

PureMVC:(注意点纪录):

1。pureMVC中的command、meditor、proxy,一般是要先注册然后才能调用。

2。command、meditor、proxy都继承于Notifiter,所以它门都有sendNotification(发送Notification)的能力,而对于接受Notification,command可以被Notification触发,但前提是command已经被注册。 meditor,可以接受listNotificationInterests中定义的Notification name的Notification 。

3。proxy,要先于meditor被注册 ,在meditor中才可以对注册的proxy的加以引用。

  • http://puremvc.org/
  • Minimalist Framework Example
  • FLV Player with PMVC
  • Sample Gallery Application (w/source)
  • 10 Tips for Working with PMVC
  • PMVC and AMFPHP
  • PMVC Tips (blog posts)
  • Cairngorm to PMVC Example
  • WebOrb/PHP/Flex/PMVC Example
  • Flex PopUps and PMVC
  • Project Template for FlashDevelop
  • PMVC Presentation (files coming Q1 ‘08)
  • Site Developed with PMVC
  • PureMVC的十个小提示,来自:http://www.duzengqiang.com/blog/article.asp?id=674

    Jesse Wardens 的一篇名为 “10 Tips For Working With Cairngorm” 的文章给了我写这篇 PureMVC 的10个小提示的灵感. 我已经用PureMVC六个月了,而且我现在很喜欢它(这已经是一个公开的秘密了).不过有言在先,下面的所有见解都只是基于我的个人经验而已.

    1. 用(Pure)MVC的思想去思考

      我应该如何开始使用(Pure)MVC呢? 一句话:用(Pure)MVC的思想去思考! 从它的名字中就可以看出, PureMVC 是基于一般的 Model-View-Controller 元设计模式的.使用Facade-pattern 这种模式你不需要直接初始化一个核心行为(instantiate the core actors), 但是PureMVC中的每个Class都有它自己的定义很清楚的角色:
      - Proxies = Model
      - Mediator and its ViewComponents = View
      - Commands = Controller

    2. 为View Components创建API

      一个 View Component可能是一个标准的UI component (比如DataGrid) 或者自定义组件 (比如一个游戏世界) 或者其他的东西. 不要直接使用它的 public方法. 而是把改变它的状态或者行为(等允许被外部调用的各种方法属性)写成API.

      PureMVC的一个有点就是可以与所使用的技术无关. 举个例子:我建了一个基于PureMVC的’Pure’ Flash application ,没有用到Flex Framework. 而后,为了使用AIR里面的File System API这个Application被转成一个AIR application.这时只需将View Components转化为使用 Flex Framework即可, 其他的Mediators 或者任何PureMVC中的任何actors均不需改变 .

    3. 多个View Components共同使用一个Mediator

      为了紧密协调多个View Components 仅使用一个Mediator. 换句话说: 不是所有的Views 都需要Mediator. 例如: 有一个ApplicationControlBar,其中包含一个TextInput ,一个Button 或者其他的组件. 然后为ApplicationControlBar 创建一个名为ApplicationControlBarMediator的 Mediator 并且把它指定给ApplicationControlBar中所包含的所有View Component.

    4. 让Events bubble up起来

      如果你不想在一个Mediator中用多个View Components 又会怎样呢?为了处理多个 View Components的用户交互事件, 我们必须把View Component里面嵌套的所有组件的事件bubble up起来.

      例如: 当你点击View Component 里面的任意Button都会触发Mediator监听的一个自定义事件. 所以Mediator并不需要知道这个Button是否存在或者任何一个这个 View Component的其他Child,它只需要知道这个事件已经被触发了就可以.

    5. 尽可能地的用Notifications通信

      Notifications是PureMVC 里面的“Events” . 当Model, View and Controller 三者之间的通信是下面几种情况时应该尽可能地的使用这个Notifications :
      (通信 from -> to)
      - Mediator -> Proxy (via mapped Commands)
      - Proxy -> Mediator
      - Proxy -> Command
      - Commands -> Mediator

      即使可以从Mediator获得Proxy,也不要直接用Mediator来改变Proxy.应该是用一个mapped Command来发送Notification. 不通过使用Command (Controller)而用Mediator (View)来直接改变 Proxy (Model) 是一种非常糟糕的方法.

    6. 尽可能多的使用 Commands / MacroCommands

      Commands在控制端做这些工作: Retrieving and interacting Proxies, 与Mediators通信或者执行其他Commands. 即使一个Command仅被用了一次或者只有两行代码也要尽可能多的使用它. 为了在你的Application中可以随时随地的再次执行一个Command ,仅需发送一个Notification.以后也可以很容易的用更复杂的actions来扩展这个 Command. 还有非常重要的一点就是你总是知道改变Proxy (Model)的actor是哪一个.

      问题: 你也没有遇到过需要按照特定次序执行多个Command的情况呢? 使用MacroCommands可以顺序执行多个SubCommands (也就是 “简单” Commands) .

    7. 使用Remote Proxy来接收和发送服务端数据

      在Application中的发送和接收数据的Proxies 叫做“Remote Proxies”. 它不是一种特殊的PureMVC Proxy, 只是一个基于Proxy的location,而这个Proxy是负责处理比如HTTPSerivice,RemoteObject或者其他服务端调用的Proxy.

      例如: 为了调用服务器端一个负责登录用户的RemoteObject 而创建了一个叫做LoginProxy的Proxy. LoginProxy负责所有与服务器端通信的工作, 也就是接收和发送数据. 当你为LoginProcess改变服务器端执行操作时,你值需要改变Application中的一个locationt即可,即 LoginProxy.

    8. 去掉没有用到的Mediators

      在某些情况下你不再使用一个Mediator和它的View Components. 你应该用facade.removeMediator(MyMediator.NAME)去掉这个Mediator同时用 destroy()来去掉包含所有listeners,timer,references的ViewComponent.以便更好的进行 垃圾回收.

    9. VO’s (Value Objects)的魅力所在

      当然在Model中存放数据的是Proxies. 而且View Components不需要知道Facade和这个PureMVC application的其他部分. 这就意味着View Component不会直接访问Model的数据.

      为了避免在View Component中存放数据可以使用一个 名为Value Objects (VO’s)的引用 . VO’s并不是PureMVC里面的核心actor,它和Flex里面的Data Binding有点渊源,是一个可以在不打破规则的情况下改变Model的数据的非常强大的方法..

    10. 可以找到的课件

      Cliff Hall 做个一个值得敬畏的工作: 不仅有非常好的文档 “Framework Overview“, “Best Practices” and a “Conceptual Diagram“, 而且还有非常非常有用的 课件. 自己去看看吧!

    分类: 未分类 标签:

    example using the PureMVC Framework

    2008年2月25日 评论已被关闭

    原文:http://www.as3dp.com/2007/12/27/minimalist-mvc-example-using-the-puremvc-framework/#more-52

    纯属个人学习而翻译…..还没有完.
    PureMVC框架初级入门

    几个月前,我在寻找一种流线型的as程序开发框架,
    CairngormPureMVC 是比较成熟的框架,PureMVC对我映像比较深刻,因为他有可靠的文档。PureMVC 带有一个概念图表,解释了框架使用的设计模式,那是我非常熟悉的。Cairngorm 没有类似的图表来解释它的结构。

    这里的例子虽然不是一个完整的应用程序,但是,是最好的用来解释PureMVC框架的内部工作的例子。在开始之前,了解框架的一些重要介绍会有帮助的。

    你触摸不到它们-模型,视图,控制器

    虽然PureMVC是建立在传统的MVC模式,你不能建立或者直接操作这些实体(模型-视图-控制器)。它们都作为单独者了,本质上是隐藏的。你不用开发它们的类,由 proxy,mediator,command 类来担任程序中的通讯。概念图表中显示了这些。

    “成双协作”用于孤军作战

    Cliff 介绍了“成双协作”的概念。在传统的MVC模式中,Model 包含了数据和商业逻辑;View 表现为用户界面的组件;Controller 操作用户的输入和操作 model。无论如何,在PureMVC中,它们都不能直接存取,它们由其它的成双设计模式来提供更多的存取和loose-coupling 用于传统的设计模式。

    Model-Proxy 成双协作:提供存取和操作程序的数据。你用model开发和注册一个proxy类来提供控制存取一个数据对像。例如:一个proxy可以从硬盘上加载数据文件,或者提供访问一个远程数据库或者web service。proxy模式提供了一个接口给程序,隐藏了详细的存取和操作数据。

    View-Mediator 成双协作:展示用户界面元件和更新。你开发有形的Mediator 类注册到View 来提供与UI组件通讯的程序接口。一个单独的Mediator类可以有多个UI组件。例如:一个登录屏幕可以有一个mediator 类,可以包含用户名和密码框和一个发送按纽。Mediator模式可以隐藏复杂的UI元件之间的交互。

    Command-Controller 成双协作:UI组件可以操作model。在传统的MVC程序中,Controller 在strategy 模式中担当一个strategy 。在这里,controller 特殊的执行决定程序怎么样对用户输入作出反应。例如,controller可以检查用户输入的有效性和决定如果程序数据需要被更新。在PureMVC,strategy 代码被嵌入Command对像中。这在本质上隐藏proxy类和proxy方法(呼叫操作mediator的数据对像),减轻mediator 和proxy之间的沟通。

    在Events之前思考Notifications

    mediators, proxies and commands三者之间交谈是用一个唯一消息经过方案叫:Notifications(通知)。通知本质上触发,它可以传送数据。Commands 发送明确的通知。只有mediators侦听通知。所以,一个proxy或者command可以发送通知,触发一个mediator去更新一个UI组件。Proxies可以发送,但是不会侦听通知。一个mediator可以操作程序数据,通过发送一个通知来触发一个command去更新数据对像通过proxy。

    事件在flash和flex中都没有丢弃,mediators可以注册和侦听从UI组件来的事件,像按纽和输入框。proxies注册和侦听的事件:当处理数据对像,像当加载一个XML数据文件时。

    Facade 是你的朋友

    你必须建立一个有形的Facade 类,让整个框架更容易使用。Facade提供一个单独的类:统一PureMVC工作所需的所有方法。另外,实例化一个facade,建立一个model,view,controller,初始化框架

    在开始建立一个PureMVC程序时,这四个重大的概念需要明白。

    The Minimalist Example入门例子

    这个例子使用Flash CS3开发,基本的侦听用户按键,储存最后按下的键和显示键。这个例子是用PureMVC 1.6写的,
    确保你已下载了框架和它已出现在你的类路径中。这个例子的包层次展示如下:

    screenshot-01-4

    下面这个图表展示了类和通知消息。开始程序时,呼叫 starup()方法,在Facade类实例ApplicationFacade中。数字显示动作发生的次序:

    conceptual-diagram2-1 

    1. ApplicationFacade 类广播了一个 STARTUP Notification;
    2. STARTUP Notification 触发了command对像:StartupCommand(来注册KeyDataProxy proxy和 stagemediator mediator)
    3. StageMediator mediator 注册了一个KEY_DOWN 事件操作者到舞台,来截取按下的键。它也注册用来接受KEYUPDATE Notifications
    4. 用户按下一个键,产生一个KEY_DOWN Event
    5. StageMediator broadcasts a KEYDOWN Notification with the character code of the key in the notification body.
    6. The KEYDOWN Notification triggers the StoreKeyCommand command object, which accesses the KeyDataProxy proxy and updates the model (data object).
    7. The KeyDataProxy proxy stores the data in a data object (just a property in the proxy in this case).
    8. The KeyDataProxy proxy broadcasts a KEYUPDATE Notification with the character code of the key in the notification body.
    9. The KEYUPDATE Notification handler in StageMediator intercepts the KEYUPDATE Notification and traces the key value.

    The StageMediator mediator could have directly updated the model in step 5 through the KeyDataProxy proxy, without going through a command object. However, this introduces a tighter coupling between the Mediator and Proxy that should best be avoided.

    The Concrete Facade

    The first step is to create a concrete Facade class called ApplicationFacade to declare notification names and bind them to command objects.

    The ApplicationFacade class defines the STARTUP, KEYDOWN and KEYUPDATE notification name constants. STARTUP and KEYDOWN are bound to the StartupCommand and StoreKeyCommand comand objects. In contrast, the KEYUPDATE notification is not bound to a command object. It is broadcast by the proxy to mediators informing them that the model has been updated.

    The Facade is the entry point into the application and defines the startup() method that broadcasts a STARTUP Notification to the rest of the application. The startup() method has to be called from the document class of your Flash document (see Document Class below). Typically, the argument passed to the startup method should be the stage instance.

    ApplicationFacade.as

    package {   import org.puremvc.interfaces.IFacade;
        import org.puremvc.patterns.facade.Facade;
        import org.puremvc.patterns.observer.Notification;
        import controller.*;  
    public class ApplicationFacade extends Facade implements IFacade { 
    // 通知名
            public static const STARTUP:String = “startup”;
            public static const KEYDOWN:String   = “key down”;
            public static const KEYUPDATE:String = “key update”; 

    // Singleton ApplicationFacade Factory Method 工厂方法
            public static function getInstance(): ApplicationFacade {
                if (instance == null) {
                    instance = new ApplicationFacade( );
                }
                return instance as ApplicationFacade;
            }
    // 广播STARTUP Notification
            public function startup(app:Object):void {
                notifyObservers(new Notification(STARTUP, app));
            }   // Register Commands with the Controller
            override protected function initializeController():void {
                super.initializeController();
                registerCommand(STARTUP, StartupCommand);
                registerCommand(KEYDOWN, StoreKeyCommand);
            }
        }
    }

    The KEYDOWN notification is sent by the StageMediator Mediator (see below) when a user presses a key. The KEYUPDATE notification is sent by the KeyDataProxy Proxy (see below) to all mediators informing them that the model has changed.

    KEYDOWN 通知是由StageMediator 发送的(当用户按下一个按键时)。KEYUPDATE 通知是由KeyDataProxy 发送给所有mediator,告诉他们models已经改变了。

    The Commands

    这里有两个command对像:
    StartupCommand和StoreKeyCommand

    StartupCommand.as

    package controller {  
    import flash.display.Stage;
        import org.puremvc.interfaces.ICommand;
        import org.puremvc.interfaces.INotification;
        import org.puremvc.patterns.command.SimpleCommand;
        import view.*;
        import model.*; 

    public class StartupCommand extends SimpleCommand
    implements ICommand {
            // Register the Proxies and Mediators
            override public function execute(note:INotification):void{
                // Create and register proxy
                facade.registerProxy(new KeyDataProxy());
                // Create and register StageMediator with the
    stage instance as an
    argument
                var stage:Stage = note.getBody() as Stage;
                facade.registerMediator(new StageMediator(stage));
            }
        }
    }

    When the StartupCommand command object is triggered by the STARTUP notification it first registers the proxy called KeyDataProxy. It then registers the StageMediator Mediator and passes the stage to it. This is an essential practice in Flash applications as the Mediators require access to the stage to add UI components to it. Note that the StartupCommand command object expects the STARTUP notification to contain a reference to the stage in the notification body.

    StoreKeyCommand.as

    package controller {   import flash.display.Stage;
        import org.puremvc.interfaces.ICommand;
        import org.puremvc.interfaces.INotification;
        import org.puremvc.patterns.command.SimpleCommand;
        import model.*;

    public class StoreKeyCommand extends SimpleCommand
    implements ICommand {

    override public function execute(note:INotification):void    {// Get the KeyDataProxy proxy var keyDataProxy:KeyDataProxy =

    facade.retrieveProxy(KeyDataProxy.NAME) as KeyDataProxy; // Set the char code of the key pressed in

    KeyDataProxy var charCode:uint = note.getBody() as uint; keyDataProxy.key = charCode; } } }

    The StoreKeyCommand comand object is triggered by the KEYDOWN notification that is sent by the StageMediator Mediator (see below). The KEYDOWN notification contains the character code of the pressed key in the message body. The command code retrieves the KeyDataProxy using the retrieveProxy() method and stores the character code using a setter method.

    The Mediator

    The example application just has one Mediator class called StageMediator that mediates interactions with the stage. It registers a Key Down even handler called onKeyDownEvent() with the stage. It also registers to receive KEYUPDATE notifications by overriding the listNotificationInterests() method. It also overrides the handleNotification() method to intercept and act on KEYUPDATE notifications.

    StageMediator.as

    package view {   import flash.events.KeyboardEvent;
        import flash.display.Stage;
        import org.puremvc.interfaces.IMediator;
        import org.puremvc.interfaces.INotification;
        import org.puremvc.patterns.mediator.Mediator;

    // Mediator for interacting with the Stage.
        public class StageMediator extends Mediator
    implements IMediator {

    // Cannonical name of the Mediator
            public static const NAME:String = ‘StageMediator’;

    // Constructor
            public function StageMediator(viewComponent:Stage) {
                // pass the viewComponent to the superclass
                super(viewComponent); 
    // Listen for events from the view component
                viewComponent.addEventListener(
    KeyboardEvent.KEY_DOWN, onKeyDownEvent);
            }   // Key down event handler
            private function onKeyDownEvent(event:KeyboardEvent):void {
                sendNotification(ApplicationFacade.KEYDOWN, event.charCode);
            }   // Return list of Nofitication names that Mediator is
    interested in
            override public function listNotificationInterests():Array {
                return [ApplicationFacade.KEYUPDATE];
            }   // Handle all notifications this Mediator is interested in
            override public function handleNotification(
    note:INotification):void  {
                switch (note.getName()) {
                    case ApplicationFacade.KEYUPDATE:
                        var charCode:uint = note.getBody() as uint;
                        trace(charCode);
                        break;
                }
            }   // Get the Mediator name
            override public function getMediatorName():String {
                return NAME;
            }
        }
    }

    The StageMediator mediator broadcasts a KEYDOWN Notification when it receives a KEY_DOWN even from the Flash event model. The character code of the key pressed is sent in the body of the Notification. On receipt of a KEYUPDATE Notification, the StageMediator simply traces it to the output panel (to keep things simple). It could have updated a UI element (e.g. Text Field) to show a change.

    The Proxy

    The minimalist example has one proxy called KeyDataProxy keeps track of the last key pressed by the user. The character code of the key is stored in the lastKeyPressed property in the proxy itself. The proxy could just have easily stored this value in an external data file or remote database if needed.

    KeyDataProxy.as

    package model {   import org.puremvc.interfaces.IProxy;
        import org.puremvc.patterns.proxy.Proxy;

    public class KeyDataProxy extends Proxy implements IProxy {

    // Cannonical name of the Proxy
            public static const NAME:String = ‘KeyDataProxy’;

    private var lastKeyPressed:uint = undefined;   // Constructor
            public function KeyDataProxy() {
                super(NAME);

    // pass the cannonical name to the superclass
            }   public function set key(charCode:uint):void {
                if (lastKeyPressed != charCode) {
                    lastKeyPressed = charCode;
                    // Create and send a KEYUPDATE Notification and pass the
    charCode as an argument
                    sendNotification(ApplicationFacade.KEYUPDATE,
    charCode);
                }
            }
        }
    }

    Note that the model stores the new character code only if it is different from the one already stored. It also doesn’t broadcast a KEYUPDATE Notification unless this is the case. This is why we needed two Notification messages, one to broadcast that a key has been pressed (KEYDOWN) and another to inform all interested mediators that the model has been updated with a new character code (KEYUPDATE).

    The Document Class

    The document class of the Flash document initializes the concrete facade and broadcasts the STARTUP notification.

    Main.as

    package {   import flash.display.MovieClip;   /**
        *   Main Class
        *   @ purpose:      Document class for movie
        */
        public class Main extends MovieClip { 
    public function Main() {
                // Instantiate the ApplicationFacade
                var facade:ApplicationFacade =
    ApplicationFacade.getInstance(); 
    // Call the application startup method and pass the
    stage as the argument
                facade.startup(this.stage);
            }
        }
    }

    So, there you have it, a minimalist example in PureMVC implemented in the Flash CS3 environment. Make sure you Disable keyboard shortcuts from the Control menu when testing the application. The source can be downloaded below.

    MVC_minimalist_v1.zip.

    I’ll follow this up with another post extending the minimalist application a little bit (e.g. adding more UI elements and mediators) to demonstrate the value of the design decisions that went into PureMVC. This post is quite long and I’m sure there are some errors – comments and corrections are welcome.

    分类: 生活杂谈 标签: