小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

flex框架pureMVC的使用:第一步

 昵稱1974760 2010-07-19

    做flex做久做大了,使用一個框架便是自然而然的事情。這樣程序才會更健壯,更易于擴展,更易于維護。pureMVC足夠簡單,核心也只有十來個類,是一個輕量級的Flex框架,只一天的時間,就可以學通,沒有理由不用它的。

 

    麻雀雖小,五臟俱全,pureMVC,直譯過來就是“純MVC”,是一個MVC框架。官方的中文文檔有44頁,放在附件中,可以下載了看。推薦一個入門的文章給大家。(http:///showtopic-11011.html ),里面有足夠全面的介紹。這里我想利用一個更加簡單的圖片展示的例子來展示pureMVC的使用細節(jié)。先看效果:

 

 

 

 

  介紹一下這個應用。程序一開始便請求展示的圖片信息(包括圖片的鏈接以及名稱),得到信息后便可以通過兩個按鈕進行逐張的瀏覽。這個例子太簡單了,以致根本沒有必要去用pureMVC,這里我只是想借用它來介紹pureMVC如何使用的。

 

    如果將上面的例子假設為做菜,哈哈,就會更有意思了。兩個 Button ,一個 Image 與 Label 便是鍋碗瓢盆,而圖片的信息便是原料,而怎么樣來炒,便要看菜譜了。pureMVC 的設計者估計也是做菜的高手。這些對他們來說就是 Model (原料), View (鍋碗瓢盆), Controller (菜譜),三個都是單例模式。同時他們還設計出一個廚師,叫 Facade ,由它來調度上面三個,也是單例的。

 

    一切從 Facade 開始,它實列化 Model , View , Controller 。MVC 也找了一些幫手, Model 找到的是 Proxy 們,代理?好像翻譯得不對。不管,以后的數據就靠它了。View 找的是 Mediator,它們負責UI,Controller 找的是 Command,不過 Command 性格比較怪,做完一次任務就不干了,也就是短生命同期的。每次用它 pureMVC 都會重新創(chuàng)建。所以只能用它來做業(yè)務邏輯,而不要將長期的數據放在里面。

 

    pureMVC 消息采用的觀察者模式,每次做完一件事情,就可以向外發(fā)出一個 Notification  (不是 Flex 里的 Event 喲),就像向外宣布“事情做完了”,具體誰關心就管不著了。這里 Proxy 有點特別,它們只會向外發(fā)出 Notification ,但從不接收,只關心自己的數據模型。這很有意義,想想人家叫 “純MVC” 嘛。

 

    好了,開始做這個例子。剛開始學東西的時候總會發(fā)現,文檔都看懂了,東西好像都會了。真要用它做東西,卻不知道怎么下手。所以還要再好好分析下程序,看看怎么樣下手。

 

   首先界面上就四個控件,兩個Button ,一個 Image 和 一個Label ,從功能上可以分為兩類,兩個 Button 是用來作控制的, Image 與 Label 用來顯示圖片及圖片名稱。和 UI 對應的是 Mediator ,所以要定義兩個 Mediator 類。如果程序變大了,也可以這樣來劃分,千萬不要為每個按鈕作一個 Mediator ,也不要整個 Application 才一個 Mediator,最好按功能來劃分。

 

   然后再來看 Model ,本例中的數據只有一項,便是要顯示的圖片資料。所以只要求定義一個 Proxy 用來數據交互就夠了。flex 得到數據的方式有許多種,如HttpService ,RemoteObject ,XMLSocket等,但不管什么,總之都是從其他地方得到有用的信息,然后將它變成自己自己的程序能夠理解的形式。當然解析數據,大多是業(yè)務邏輯( Controller )的工作了。

 

   再看Controller這邊,Controller 涉及到業(yè)務 ,本例中業(yè)務有程序啟動(StartUp),還有個就是得到圖片信息。還有嗎,好像沒有了。兩個Command就可以應付了。

 

    好了,可以動手編碼了。

 

定義兩個Mediator (ImageMediator 與 ControlBtnsMediator,繼承Mediator類,實現IMediator接口),

兩個Command ( StartUpCommand 與 GetUrlListCommand,繼承SimpleCommand類,實現ICommand) 

以及一個Proxy (ImageUrlListProxy ,繼承Proxy,實現IProxy接口)

一個圖片信息類(ImageUrlVO),用來存放單張圖片信息。

一個Facade (MyAppFacade 繼承Facade,實現IFacade接口)

 

程序的包結構:

 

同時思考需要的 Notification ,它是將整個框架聯系起來的關鍵。

1.程序開始便要啟動 StrartUpCommand,所以StrartUpCommand 要關注 "app_startup"

2.StrartUpCommand主要完成Proxy與Mediator的注冊,完成后便可以啟動GetUrlListCommand,所以GetUrlListCommand應關注"app_startup_over"

3.GetUrlListCommand 通過 ImageUrlListProxy去獲取圖片信息,前面提到 ImageUrlListProxy是不能接收Notification,所以GetUrlListCommand要直接調用ImageUrlListProxy的public成員函數loadUrlList()去獲取圖片信息

4.ImageUrlListProxy 得到圖片鏈接以后,便可以對外宣布“圖片信息已經得到了”,即對外Send一個"url_load_complete"的Notification,關注這一Notification的自然是ImageMediator,它直接將圖片信息保存起來,并顯示第一張圖片內容

5.ControlBtnsMediator不需要關注任何Notification,不過點擊兩個按鈕時會向外Send Notification ("next_image" 與 "prev_image"),,通知顯示下一張或上一張圖片。關注這兩個Notification的自然是ImageMediator了。

 

好了,流程都介紹完了,來看代碼。

 

先定義類 ImageUrlVO 的代碼如下:

Java代碼 復制代碼
  1. package MyApp.Model.VO   
  2. {   
  3.     public class ImageUrlVO   
  4.     {   
  5.         public var url:String;    //圖片鏈接   
  6.         public var name:String;  //圖片名稱   
  7.         public function ImageUrlVO(url:String,name:String){   
  8.             this.url = url;   
  9.             this.name = name;   
  10.         }   
  11.     }   
  12. }  

 

    接下來從程序的執(zhí)行步驟依次看各個類的代碼。

 

    主界面 HelloPureMVC.mxml:

Xml代碼 復制代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"  
  3.   width="200" height="200" creationComplete="initApp()">    
  4.   <mx:Script>  
  5.     <![CDATA[  
  6.         import MyApp.MyAppFacade;  
  7.         public function initApp():void{  
  8.             var facade:MyAppFacade = MyAppFacade.getInstance();  
  9.             facade.startup( this );  
  10.         }  
  11.     ]]>  
  12.   </mx:Script>  
  13.  <mx:Canvas id="mainContainer" width="100%" height="100%">  
  14.   <mx:Label id="nameLabel" x="87.5" y="0"/>  
  15.   <mx:Image id="image" x="30" y="20" width="140" height="140"/>  
  16.   <mx:Button x="10" y="168" label="上一張" id="btnPrev"/>  
  17.   <mx:Button x="125" y="168" label="下一張" id="btnNext"/>  
  18.     
  19.  </mx:Canvas>  
  20. </mx:Application>  

  主界面現在只關注布局就夠了。同時還要注意到里面的initApp()函數,它首先得到Facade實例,再調用其 startup() 函數啟動整個PureMVC框架。

  跟進去再讓看 MyAppFacade 的實現。

 

Java代碼 復制代碼
  1. package MyApp   
  2. {   
  3.     import MyApp.Controller.GetUrlListCommand;   
  4.     import MyApp.Controller.StartUpCommand;   
  5.     import org.puremvc.as3.interfaces.IFacade;   
  6.     import org.puremvc.as3.patterns.facade.Facade;   
  7.   
  8.     public class MyAppFacade extends Facade implements IFacade   
  9.     {   
  10.         public static const APP_STARTUP:String = "app_startup";   
  11.         public static const APP_STARTUP_OVER:String = "app_startup_over";   
  12.            
  13.         public function MyAppFacade()   
  14.         {   
  15.             super();   
  16.         }   
  17.            
  18.         public static function getInstance():MyAppFacade{   
  19.             if(instance==null) instance = new MyAppFacade();   
  20.             return instance as MyAppFacade;   
  21.         }                                      
  22.         override protected function initializeController():void{   
  23.             super.initializeController();   
  24.             //register some Commands   
  25.             registerCommand(APP_STARTUP,StartUpCommand);   
  26.             registerCommand(APP_STARTUP_OVER,GetUrlListCommand);   
  27.         }   
  28.         public function startup(app:Object):void{          
  29.             sendNotification(APP_STARTUP,app);     
  30.         }      
  31.            
  32.     }   
  33. }  

    可見Facade做的事情很簡單, initializeController() 是用來初始化Controller的,這個函數是建立各個Notification與Command映射的地方,有了上面的流程分析,

Java代碼 復制代碼
  1. registerCommand(APP_STARTUP,StartUpCommand);   
  2. registerCommand(APP_STARTUP_OVER,GetUrlListCommand);  

這兩行這很容易了。startup()函數,終于輪到它了。它只做了一件事情,就是向外派發(fā)一個APP_STARTUP的Notification,關注它的是前面已經建立映射的StartUpCommand,pureMVC會實例化一個StartUpCommand的實例,并將app作為參數,調用其execute函數。

tip:通常用一個字符串來標識一個Notification,不過建議用字符常量。減少犯錯的可能。

 

 再來看StartUpCommand的代碼:

Java代碼 復制代碼
  1. package MyApp.Controller   
  2. {   
  3.     import MyApp.Model.ImageUrlListProxy;   
  4.     import MyApp.MyAppFacade;   
  5.     import MyApp.View.ControlBtnsMediator;   
  6.     import MyApp.View.ImageMediator;   
  7.     import org.puremvc.as3.interfaces.ICommand;   
  8.     import org.puremvc.as3.interfaces.INotification;   
  9.     import org.puremvc.as3.patterns.command.SimpleCommand;   
  10.   
  11.     public class StartUpCommand extends SimpleCommand implements ICommand   
  12.     {   
  13.         public function StartUpCommand()   
  14.         {   
  15.             super();   
  16.         }          
  17.         override public function execute(notification:INotification):void  
  18.         {   
  19.             var app:HelloPureMVC = notification.getBody() as HelloPureMVC;   
  20.             //注冊代理(proxy)   
  21.             facade.registerProxy( new ImageUrlListProxy( ImageUrlListProxy.NAME ) );   
  22.             //注冊中介器   
  23.             facade.registerMediator( new ImageMediator(    
  24.                    ImageMediator.NAME,    
  25.                    {   
  26.                      image:app.image,   
  27.                      nameLabel:app.nameLabel   
  28.                    }   
  29.             ) );   
  30.                
  31.             facade.registerMediator( new ControlBtnsMediator(   
  32.                     ControlBtnsMediator.NAME ,   
  33.                     {   
  34.                         btnNext:app.btnNext,   
  35.                         btnPrev:app.btnPrev   
  36.                     }   
  37.             ) );   
  38.             //通知已經初始化完畢   
  39.             sendNotification(MyAppFacade.APP_STARTUP_OVER,app);                
  40.         }   
  41.            
  42.     }   
  43. }  

    只有一個execute()函數,它的任務便是注冊前面提到的兩個Mediator和一個Proxy,用到的是registerProxy()與registerMediator()兩個函數,完成注冊后便可以對外Send MyAppFacade.APP_STARTUP_OVER 了。關注這一Notificator的便是前面已經建立映射的 GetUrlListCommand 。同樣一個 GetUrlListCommand 會被實例化,再調用其execute()函數。

 

tip:Proxy類的構造函數需要一個proxyName:String作為其唯一標識,可以通過這一字符串得到該Proxy的引用,這里也建議使用字符常量

 

Java代碼 復制代碼
  1. package MyApp.Controller   
  2. {   
  3.     import MyApp.Model.ImageUrlListProxy;   
  4.     import org.puremvc.as3.interfaces.ICommand;   
  5.     import org.puremvc.as3.interfaces.INotification;   
  6.     import org.puremvc.as3.patterns.command.SimpleCommand;   
  7.   
  8.     public class GetUrlListCommand extends SimpleCommand implements ICommand   
  9.     {   
  10.         public function GetUrlListCommand()   
  11.         {   
  12.             super();   
  13.         }   
  14.            
  15.         override public function execute(notification:INotification):void  
  16.         {   
  17.             //得到圖片鏈接   
  18.             (facade.retrieveProxy( ImageUrlListProxy.NAME ) as ImageUrlListProxy).loadUrlList();           
  19.         }   
  20.            
  21.     }   
  22. }  

    好簡單,呵呵,得到 ImageUrlListProxy 的實例,調用其loadUrlList()函數就可以了。前面提到Proxy不會去接收任何Notification,所以只能通過調用其成員函數的形式來使用它。

 看看ImageUrlListProxy的代碼:

 

Java代碼 復制代碼
  1. package MyApp.Model   
  2. {   
  3.     import MyApp.Model.VO.ImageUrlVO;   
  4.     import org.puremvc.as3.interfaces.IProxy;   
  5.     import org.puremvc.as3.patterns.proxy.Proxy;   
  6.   
  7.     public class ImageUrlListProxy extends Proxy implements IProxy   
  8.     {   
  9.         public static const NAME:String = "ImageUrlListProxy";   
  10.            
  11.         //定義一些Notification字符常量   
  12.         public static const URL_LOAD_COMPLETE:String = "url_load_complete";   
  13.            
  14.         public function ImageUrlListProxy(proxyName:String=null, data:Object=null)   
  15.         {              
  16.             super(proxyName,data);   
  17.         }          
  18.         public function loadUrlList():void{            
  19.             data = new Array();   
  20.             //push六張圖片的Url   
  21.             data.push(new ImageUrlVO("http://www./r/io/ioryioryzhan/pic1.jpg","卡莫"));   
  22.             data.push(new ImageUrlVO("http://www./r/io/ioryioryzhan/pic2.jpg","李時珍"));   
  23.             data.push(new ImageUrlVO("http://www./r/io/ioryioryzhan/pic3.jpg","姚明"));   
  24.             data.push(new ImageUrlVO("http://www./r/io/ioryioryzhan/pic4.jpg","費得了"));   
  25.             data.push(new ImageUrlVO("http://www./r/io/ioryioryzhan/pic5.jpg","伍茲"));   
  26.             data.push(new ImageUrlVO("http://www./r/io/ioryioryzhan/pic6.jpg","不認得"));    
  27.             //通知image Url已經全部得到了   
  28.             if(data==null)trace("data is null");   
  29.             sendNotification( URL_LOAD_COMPLETE ,data );                       
  30.         }              
  31.     }   
  32. }  

  loadUrlList()函數去得到圖片信息,由于沒有后臺,所以只能用這種直接寫硬編碼的方式了, ,會有很多方式得到數據,如前面提到的HttpService及RemoteObject等。同步的異步的都可以用。

 Proxy 有一個data成員,是個Object,用來盛放接收到的數據。得到數據后便可以Send一個Notification (URL_LOAD_COMPLETE)了。接下來看關注這個Notification的ImageMediator。

 

Java代碼 復制代碼
  1. package MyApp.View   
  2. {   
  3.     import mx.controls.Alert;   
  4.     import mx.controls.Image;   
  5.     import mx.controls.Label;   
  6.        
  7.     import MyApp.Model.ImageUrlListProxy;   
  8.     import MyApp.Model.VO.ImageUrlVO;   
  9.     import org.puremvc.as3.interfaces.IMediator;   
  10.     import org.puremvc.as3.interfaces.INotification;   
  11.     import org.puremvc.as3.patterns.mediator.Mediator;   
  12.   
  13.     public class ImageMediator extends Mediator implements IMediator   
  14.     {   
  15.         public static const NAME:String = "ImageMediator";   
  16.            
  17.         private var arrayOfImage:Array=null;    
  18.         private var currentIndex:int=-1;   
  19.            
  20.         public function ImageMediator(mediatorName:String=null, viewComponent:Object=null)   
  21.         {   
  22.             super(mediatorName, viewComponent);   
  23.         }   
  24.            
  25.            
  26.         override public function listNotificationInterests():Array{   
  27.             //列出感興趣的Notification   
  28.             return [   
  29.               ImageUrlListProxy.URL_LOAD_COMPLETE,   
  30.               ControlBtnsMediator.NEXT_IMAGE,   
  31.               ControlBtnsMediator.PREV_IMAGE           
  32.             ];   
  33.         }      
  34.            
  35.         override public function handleNotification(notification:INotification):void{              
  36.             switch(notification.getName()){   
  37.                 case ImageUrlListProxy.URL_LOAD_COMPLETE:          
  38.                     arrayOfImage = notification.getBody() as Array;      
  39.                     if(arrayOfImage){   
  40.                         trace(arrayOfImage.length);   
  41.                         trace((viewComponent.nameLabel as Label).text);   
  42.                         (viewComponent.nameLabel as Label).text = (arrayOfImage[0] as ImageUrlVO).name;   
  43.                         (viewComponent.image as Image).source = (arrayOfImage[0] as ImageUrlVO).url;   
  44.                         currentIndex = 0;   
  45.                     }else{   
  46.                         Alert.show("沒有得到圖片鏈接","錯誤");   
  47.                     }   
  48.                        
  49.                 break;     
  50.                 case ControlBtnsMediator.NEXT_IMAGE:   
  51.                     if(currentIndex==-1)break;   
  52.                     if(currentIndex >= arrayOfImage.length-1 ){Alert.show("已經是最后一張圖片了","錯誤");}   
  53.                     else{   
  54.                         trace((viewComponent.nameLabel as Label));   
  55.                         (viewComponent.nameLabel as Label).text = (arrayOfImage[currentIndex+1] as ImageUrlVO).name;   
  56.                         (viewComponent.image as Image).source = (arrayOfImage[currentIndex+1] as ImageUrlVO).url;   
  57.                         ++currentIndex;   
  58.                     }                      
  59.                 break;   
  60.                 case ControlBtnsMediator.PREV_IMAGE:   
  61.                 if(currentIndex==-1)break;   
  62.                 if(currentIndex ==0 ){Alert.show("目前是第一張圖片","錯誤");}   
  63.                     else{   
  64.                         (viewComponent.nameLabel as Label).text = (arrayOfImage[currentIndex+-1] as ImageUrlVO).name;   
  65.                         (viewComponent.image as Image).source = (arrayOfImage[currentIndex-1] as ImageUrlVO).url;   
  66.                         --currentIndex;   
  67.                     }   
  68.                 break;             
  69.                 default:break;   
  70.             }              
  71.         }                      
  72.     }   
  73. }  

  ImageMediator除了關注ImageUrlListProxy.URL_LOAD_COMPLETE外,還要關注ControlBtnsMediator.NEXT_IMAGE以及ControlBtnsMediator.PREV_IMAGE,即為示下一張或上一張圖片。

 

  具體怎么將Mediator與其關注的Notification關聯起來呢,listNotificationInterests(),就是它了。它要求返回一個字符數組,在注冊這個Mediator時,該函數就會被調用,之后,當一個Notification被發(fā)送時,如果該Notification的字符串存在于這個字符數組時,這個Mediator就能接收到。

 

 處理Notification是在handleNotification()函數內進行的,通過switch/case的方式,對不同的Notification進行不同的處理。會MFC的筒子們一定會覺得好熟悉啊,在 MFC 里,窗口函數也是這樣來處理消息的。

 

 具體代碼就不分析了,很簡單的。

 

 最后就只有ControlBtnsMediator了。

 

Java代碼 復制代碼
  1. package MyApp.View   
  2. {   
  3.     import flash.events.MouseEvent;   
  4.        
  5.     import mx.controls.Button;   
  6.        
  7.     import org.puremvc.as3.interfaces.IMediator;   
  8.     import org.puremvc.as3.patterns.mediator.Mediator;   
  9.   
  10.     public class ControlBtnsMediator extends Mediator implements IMediator   
  11.     {   
  12.         public static const NAME:String = "ControlBtnsMediator";   
  13.            
  14.         public static const NEXT_IMAGE:String = "next_image";   
  15.         public static const PREV_IMAGE:String = "prev_image";   
  16.            
  17.         public function ControlBtnsMediator(mediatorName:String=null, viewComponent:Object=null)   
  18.         {   
  19.             super(mediatorName, viewComponent);   
  20.             (viewComponent.btnPrev as Button).addEventListener(MouseEvent.CLICK,onClickPrev);   
  21.             (viewComponent.btnNext as Button).addEventListener(MouseEvent.CLICK,onClickNext);   
  22.         }          
  23.         private function onClickPrev(e:MouseEvent):void{   
  24.             sendNotification(PREV_IMAGE);   
  25.         }   
  26.         private function onClickNext(e:MouseEvent):void{   
  27.             sendNotification(NEXT_IMAGE);   
  28.         }   
  29.   
  30.     }   
  31. }  

  注冊監(jiān)聽,響應時發(fā)送相應的Notification。

  好了,都介紹完了,點擊運行吧。

    附件中有一個pureMVC的中文文檔,以及Project的源文件,pureMVC的代碼Project的源文件中。

    本站是提供個人知識管理的網絡存儲空間,所有內容均由用戶發(fā)布,不代表本站觀點。請注意甄別內容中的聯系方式、誘導購買等信息,謹防詐騙。如發(fā)現有害或侵權內容,請點擊一鍵舉報。
    轉藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多