首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 媒体动画 > flex >

Flex 框架PureMVC 源码解析之-Controller篇

2012-09-14 
Flex 框架PureMVC 源码解析之--Controller篇1、Controller实现了IController接口,提供其中方法的实现。?pack

Flex 框架PureMVC 源码解析之--Controller篇

1、Controller实现了IController接口,提供其中方法的实现。

?

package org.puremvc.as3.core{import org.puremvc.as3.core.*;import org.puremvc.as3.interfaces.*;import org.puremvc.as3.patterns.observer.*;public class Controller implements IController{/** * 作用:构造方法,Controller是一个单例对象,所以不建议直接调用构造方法 *             建议调用它的静态方法getInstance(),为什么不直接定义private级别呢,不解? * 这里我们看到了上一节讲的存储消息名称-消息处理类对象的commandMap数组 * @throws Error 如果该单例对象已经被创建则在调用构造函数时会出错 *  */public function Controller( ){if (instance != null) throw Error(SINGLETON_MSG);instance = this;commandMap = new Array();initializeController();}/** * 作用:初始化控制器,说白了是控制器拿到一个view对象,为什么要拿到view对象呢,下面有讲到 * @return void */protected function initializeController(  ) : void {view = View.getInstance();}/** * 作用:代替构造函数的工厂方法 *  * @return 返回单例的Controller对象 */public static function getInstance() : IController{if ( instance == null ) instance = new Controller( );return instance;}/** * 作用:上节稍微提到了,通过给定的消息来执行对应的处理类 *  * @param 消息对象 */public function executeCommand( note : INotification ) : void{var commandClassRef : Class = commandMap[ note.getName() ];if ( commandClassRef == null ) return;var commandInstance : ICommand = new commandClassRef();commandInstance.execute( note );}/** * 作用:在Controller绑定消息名称和消息处理类,Controller里面保存这些key-value对,并且通过view对象注册”观察者“。 *            由于pureMVC采用观察者模式,在view对象里面存着这一个数组map, *     这里面的key依旧是notificationName,value却是观察者对象; *简单提一下观察在模式:类似于一个购物网站的邮件通知,例如我们在拍拍看到一些书, *    我们用邮箱注册并且收藏了这些书,当这些书的价格变动时我们就都会收到通知。 *            这里的拍拍就相当于这个view,消息就是价格变动,我们就是观察者。 *   我们邮箱注册时这个view就用一个map存这个消息(key)和我们的邮箱(value), *           当价格变动时,这个view就遍历map查找所有注册过的邮箱给我们发消息。 * @param notificationName 消息名称 * @param commandClassRef 消息处理类 */public function registerCommand( notificationName : String, commandClassRef : Class ) : void{if ( commandMap[ notificationName ] == null ) {view.registerObserver( notificationName, new Observer( executeCommand, this ) );}commandMap[ notificationName ] = commandClassRef;}/** * 作用:检查是否存在这个消息名称对应的处理类 *  * @param notificationName 消息名称 * @return 布尔值,是否存在这个处理类 */public function hasCommand( notificationName:String ) : Boolean{return commandMap[ notificationName ] != null;}/** * 作用:移除绑定,并且移除”观察者“,以后拍拍价格变动也不通知你了:) *  * @param notificationName 消息名称 */public function removeCommand( notificationName : String ) : void{if ( hasCommand( notificationName ) ){view.removeObserver( notificationName, this );commandMap[ notificationName ] = null;}}protected var view : IView;protected var commandMap : Array;protected static var instance : IController;protected const SINGLETON_MSG : String = "Controller Singleton already constructed!";}}
?

?

热点排行