Skip to main content

PureMVC 我也會 [4]

Command
  • 當然就是 Command Design Pattern 的實作
  • 用註冊的方式 mapping Notification
  • 一般來說 Command 的生命週期只有到它的 execute 方法執行後就掛了(揮手帕~)
  • 環保!免洗!能用就儘量用!!

Command Design Pattern
顧名思義當然是「命令」設計模式了。這個模式非常好理解,為什麼叫命令?意思就是你老媽叫你去洗碗、掃地或做其他家事,不都是一個指令一個動作?如同一支電視遙控器,每按一個按鈕都有對應的命令,按了電源鈕 -> 開機,音量放大 -> 音量變大一點,選台 -> 跳台...動作做完就結束,所有命名都是有內容的,如果每一個命令都要乖乖的命名的話,光記內容不就累死了...所以你會發現繼承 Command 後,只需要 override execute():void 方法即可。

PureMVC 送你用的 Command 有兩種,SimpleCommand and MacroCommand。SimpleCommand 當然就是單次使用啦!想要一次執行一堆請愛用 MacroCommand,如果你還需要其他執行模式,可以自己手工建立,或看官網所提供的 Utilities。

StartupCommand:
官方範例中的 StartupCommand 使用 MacroCommand,就是為了分開 MVC 相關初始用 Commands

package com.me.myapp.controller
{
import org.puremvc.as3.interfaces.*;
import org.puremvc.as3.patterns.command.*;
import com.me.myapp.controller.*;
// A MacroCommand executed when the application starts.
public class StartupCommand extends MacroCommand {
// Initialize the MacroCommand by adding its subcommands.
override protected function initializeMacroCommand() : void {
addSubCommand( ModelPrepCommand ); //Model 相關準備用 Command
addSubCommand( ViewPrepCommand ); // View 相關準備用 Command
}
}
}


MacroCommand 使用:

addSubCommand( command:Class );

來加入馬上要執行的 command class

你也會發現有人會這樣寫:

package com.mvc.controllers
{
import com.mvc.models.ListProxy;
import com.mvc.views.ListMediator;

import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.command.SimpleCommand;

public class StartupCommand extends SimpleCommand
{
public function StartupCommand()
{
super();
}
override public function execute(notification:INotification):void{
facade.registerMediator( new ListMediator("ListMediator", notification.getBody()) );
facade.registerProxy( new ListProxy());
//直接都在這邊處理初始註冊
}
}
}

要使用 SimpleCommand or MacroCommand 完全是看專案規模與個人喜好應用就可以了。

寫完後當然就是要用 facade.registerCommand(); 將 "STARTUP" 與 StartupCommand mapping 起來。等到有人 sendNotification( STARTUP, body ); 後,程式自然會幫你執行 StartupCommand。

註冊 Command 的地點沒有規定一定要在 ApplicationFacade 或 StartupCommand 內,不過建議還是由其他 Command 處理,別四處亂寫是比較安全。

Command 的功用
  • 初始註冊事宜
  • 通常用來處理 multi-view 共用的 proxy 相關操作,免得每個 mediator 都抓著同一個 proxy 不放。
    如:想要取用 DataProxy 的 getList():void 方法。這時候只要將處理 DataProxy 用的 DataProxyCommand 跟 "GET_DATA_LIST" mapping 起來,所有想要 DataProxy.getList() 資料的 views 都可以利用 sendNotification( "GET_DATA_LIST" ); 到時候如果 getList() 改名了又或者改用其他 Proxy 的方法,也只需要修改 DataProxyCommand 內容就夠了。如:

    package com.controls
    {
    import com.models.DataProxy;
    import org.puremvc.as3.interfaces.INotification;
    import org.puremvc.as3.patterns.command.SimpleCommand;
    public class DataProxyCommand extends SimpleCommand
    {
    override public function execute( notification:INotification) :void
    {
    //多個 Notification 共用同一個 Command 就是用 if else or switch 處理
    if( notification.getName() == "GET_DATA_LIST" ){
    var proxy:DataProxy = facade.retrieveProxy( DataProxy.NAME ) as DataProxy;
    //用 facade.retrieveProxy 取出 DataProxy
    proxy.getList();
    }
    }
    }
    }

  • [自己填...]

其實 Command 的應用層面很大,能做的事情琳琅滿目,請大家花時間好好的跟它認識吧!

Comments

Post a Comment

Popular posts from this blog

[Flex] PureMVC standard with Spring extensions

由於上次稍微玩了一下 Robotlegs 依賴注入(DI) 主導的 MVC 框架,而著名也使用依賴注入的 Java / Java EE 的 Spring framework 出了 for ActionScript 的版本,剛好在最近 Spring ActionScript 1.0 正式 release 了(想了解 Spring 是啥咪東東的話請自行找 google 大神),這個版本除了基本框架外,也包含了 Cairngorm 與 PureMVC 的外掛...想當然耳,就拿來測試一下用在 PureMVC 內的感覺囉!! 參考了 官方範例 中 PureMVC 唯二的範例原始檔,以下使用的是「設定檔依賴注入 facade 透過 addConfigSource() 的方式來 init 」:(其實除了 embed 外,都是外部載入) Online Demo with source code 工作環境:FlashBuilder, Flex SDK4 請下載 PureMVC Standard 版本 再下載 Spring ActionScript 最新版本後,除了 spring-actionscript-cairngorm 不需要外,都放到 /src 下(記得只需要 org 開始...),也別忘了lib 內的 swc 檔 copy 到 /libs 下 Spring 的 injection 並不像 Robotlegs 直接來個 [Inject] metadata 的自動化那樣方便,但是其冷血度(檔案的鬆偶程度)更勝後者!如果你要使用設定檔(applicationContext.xml) 來做注入的話,準備工作就挺多的...XD 依照 applicationContext.xml 內設定的方式分別寫入 constructor 或者是 setter 依賴注入(本範例統一使用 setter injection) 為了跟大家都沒關係所以都使用 interface 來處理,所以你會在範例中發現大家都有介面...(並沒有真的研究過 Spring,也許還有其他作法) 準備 compiler 時候要用的 classe。由於在 setter, getter 的寫法上都使用 interface,所以真正用到的 class 需要預先在輸出階段就打包到程式內。 基本上 PureMVC 類 class...

[AIR] JoSi FXGtoLayout

JoSi FXGtoLayout v0.3.0, Adobe AIR 3 runtime 這個又是一個 "就是" 系列懶人小工具,主要是針對 Adobe fxg 格式做分析轉成 Mobile 開發用的視圖程式碼,加速畫面配置使用。 為什麼會製作這個工具,原因主要是本人在使用的 Corona SDK 與 Titanium SDK 都沒有好用的視覺化編輯工具。一般設計師產出 layout 檔會使用 PhotoShop 來製作,在不多花錢的原則下,畫面對齊的基準就是其輸出的 fxg 資料做對應,如果要一筆一筆將資料鍵入,做久也是會膩的,所以花了點時間將這個工具做出來...