mediators, proxies and commands三者之间交谈是用一个唯一消息经过方案叫:通知。通知本质上触发,它可以传送数据。Commands 发送明确的通知。只有mediators侦听通知。所以,一个proxy或者command可以发送通知,触发一个mediator去更新一个UI组件。 Proxies可以发送,但是不会侦听通知。一个mediator可以操作程序数据,通过发送一个通知来触发一个command去更新数据对像通过 proxy。
The Events in Flash and Flex are not discarded, but used at the boundary of the application. Mediators can register and listen for events from UI components such as buttons and fields. Proxies register and listen for events when dealing with data objects such as when loading an XML data file asynchronously. 事件在flash和flex中都没有丢弃,mediators可以注册和侦听从UI组件来的事件,像按纽和输入框。proxies注册和侦听的事件:当处理数据对像,像当加载一个XML数据文件时。 The Facade is your friend Facade是你的朋友 You must create a concrete Facade class that makes the whole framework easier to use. The Facade provides a single class that unifies all the methods required to make PureMVC work. In addition, instantiating a facade initializes the PureMVC framework by creating the model, view and controller singletons. 你必须建立一个有形的Facade 类,当整个框架更容易使用。Facade提供一个单独的类:统一PureMVC工作所需的所有方法。另外,实例化一个facade,建立一个model,view,controller,初始化框架 These were the four big concepts that I needed to understand before diving into creating a PureMVC application. 在开始建立一个PureMVC程序时,这四个重大的概念需要明白。 The Minimalist Example入门例子 The example was developed using Flash CS3 and basically listens for user key presses, stores the last key pressed, and displays the key (MVC_minimalist_v1.zip). The example app was written in PureMVC Release 1.6. The package structure will change in release 2.0 and the import statements will need to be changed accordingly. 这个例子使用Flash CS3开发,基本的侦听用户按键,储存最后按下的键和显示键。这个例子是用PureMVC 1.6写的, Make sure to download and install the PureMVC framework and make it accessible in your classpath first. The package hierarchy as shown in the Flash Project Panel is shown here for reference. 确保你已下载了框架和它已出现在你的类路径中。这个例子的包层次展示如下: Here is a conceptual diagram that shows the classes and the notification messages. To start the application, call the startup() method in the the Concrete Facade class called ApplicationFacade. The numbers show the sequence of actions that take place. 下面这个图表展示了类和通知消息。开始程序时,呼叫 starup()方法,在Facade类实例ApplicationFacade中。数字显示动作发生的次序: The Concrete Facade class called ApplicationFacade broadcasts a STARTUP Notification. Facade类的实例广播了一个 STARTUP 通知; The STARTUP Notification triggers the StartupCommand command object which registers the KeyDataProxy proxy, and StageMediator mediator. STARTUP 通知触发了command对像:StartupCommand(注册为KeyDataProxy proxy和 stagemediator的 mediator), StageMediator mediator registers a KEY_DOWN Event handler to the stage to intercept key presses. It also registers to receive KEYUPDATE Notifications. User presses a key generating a KEY_DOWN Event that is intercepted by the key down event handler in the StageMediator. StageMediator broadcasts a KEYDOWN Notification with the character code of the key in the notification body. The KEYDOWN Notification triggers the StoreKeyCommand command object, which accesses the KeyDataProxy proxy and updates the model (data object). The KeyDataProxy proxy stores the data in a data object (just a property in the proxy in this case). The KeyDataProxy proxy broadcasts a KEYUPDATE Notification with the character code of the key in the notification body. 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 注册Proxies and Mediators override as3的覆盖语法关键字. ide public function execute(note:INotification):void{ // Create and register proxy 创建和注册一个proxy facade.registerProxy(new KeyDataProxy()); // Create and register StageMediator with the stage instance as an argument 以后stage为构造参数创建一个StageMediator 并且将它注册 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. 当StartupCommand 命令对象被STARTUP notification触发时,它首先注册一个KeyDataProxy类型的proxy,之后注册一个StageMediator类型的 Mediator以及传递stage参数给它.在flash应用程序中,这是一个必要的实现当Mediators要求有权使用stage将UI组建添加到 StartupCommand中. 注意 StartupCommand 命令对象期望在STARTUP notification对象中存在一个stage类型的引用. 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 设置在KeyDataProxy中键盘按下的字符码,uint是无符号32位数,此处进行强制类型转换 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. StoreKeyCommand命令对象被StageMediator Mediator 发送的KEYDOWN notification触发.在KEYDOWN notification的消息体中包含有按下键的字符码.命令程序获得KeyDataProxy对象可以使用retrieveProxy()方法而存储 字符码用一个set方法. 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的Mediator类,它和stage相互协调影响.在stage内,它将一个Key Down事件注册给onKeyDownEvent().同时还获得KEYUPDATE notifications通过覆盖listNotificationInterests()方法,它还覆盖了handleNotification() 方法拦截以及调用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. StageMediator协调者广播一个 KEYDOWN 的通知,当它在flash的事件模型中获得一个KEY_DOWN事件.在Notification的内部,按下键对应的字符码会被发送.在的到一个 KEYUPDATE Notification的收条后,StageMediator能够轻松的将它输出到面板(让事物简单).它可以更新一个UI元素(例如 Text Filed)去显示一个改变. 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的代理保存最后一个被用户按下的键的状态.这个键的字符码被存储在代理自身的lastKeyPressed属性中.代理可以很轻松的将这个值可以根据需要存放到外部的数据文件中或者远程数据库. 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). 注意model存储新的字符码只有在与之前存储的制字符码不一致的情况下.同样它也不会广播一个KEYUPDATE通知.这就是为什么我们需要2个通知消 息,一个用来广播键已经被按下,另一个用来通知所有相关的在model已经被用一个新的字符码更新的mediators.
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. 这样看来,你已经了解它了,一个在flash CS3环境下用PureMVC实现迷你邮件列表例子.确定你已经让你键盘的快捷键失效通过控制菜单当你测试这个例子的时候.源程序可以在下面下载. 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. 革命尚未成功 同志仍需努力,继这篇文章之后我会发表它的姐妹篇,将现有系统稍微扩展一下(例如 添加更多的UI组件和协调者)来演示PureMVC的设计理念.这篇文章有点长,我相信这里存在一些错误,欢迎批评指正.
转载于:https://www.cnblogs.com/zack/archive/2009/06/11/1501736.html
相关资源:各显卡算力对照表!