Skip to main content

PureMVC 我也會 [6]

Mediator
  • ViewComponents 與 pureMVC 架構的中介
  • 監聽並反應 View Component 發出的 Event
  • 可以發送與接收 Notification
  • 儘量少操作 Proxy 公開方法,多用 sendNotification...

Mediator design pattern
要多認識這個 Mediator 設計模式的話,請自行看連結說明啊! 簡單來講,假使有一個 View 裡面有好幾個 MovieClip
組成,而這些 MovieClip 會互相影響對方...這個情況在 Flash 中,通常都會變成下圖:

MovieClip 直接控制其他 MovieClip 搞到整個關係很複雜...換一個元件簡直是災難。
加入 Mediator 後,示意圖就會變成:

這樣,所有的 MovieClip 都透過 Mediator 來跟其他 MovieClip 溝通,當某一個 MovieClip 替換成別的元件,這時候也只需要修改 Mediator 中的引用即可,是不是變得很乾淨?如果同一組 MovieClip 有另外一個操作模式,也只需要替換掉 Mediator 即可!天下太平啊~~~

而 PureMVC 中就是利用 Mediator class 為與前端 ViewComponent 的中介,這樣可以切開 ViewComponent 與 PureMVC framework 的關係,不管你前端介面使用 Flash or Flex 製作都跟程式核心無關。

所以 ViewComponent 製作時只需要兩個原則,一把所有的請求都以 Event 送出由 Mediator 處理,二提供公開方法, Mediator 只需要監聽 View 的 Event,將收到的資訊透過公開方法喂進 ViewComponent 即可。
如在 ViewComponent 中:

public function setList( result:Object ):void{
list.dataProvider = result as ArrayCollection;
}

//然後在按下取得資料的按鈕 Click action 寫上:
dispatchEvent( new Event( "GET_LIST" ));

新建 Mediator 的時候一樣有幾個重點方法需要覆寫,onRegister, onRemove, listNotificationInterests and handleNotification。
處理 List ViewComponent 的 Mediator:

package com.mvc.views
{
import com.mvc.models.ListProxy;
import fl.core.UIComponent
import flash.events.Event;

import org.puremvc.as3.interfaces.INotification;
import org.puremvc.as3.patterns.mediator.Mediator;

public class ListMediator extends Mediator
{
public function ListMediator(mediatorName:String=null, viewComponent:Object=null)
{
super(mediatorName, viewComponent);
}
override public function listNotificationInterests():Array
{
//要處理啥訊息都在這邊列好,列出有興趣的 Notification
return [
ListProxy.LIST_CHANGED
];
}
override public function onRegister():void{
//監聽 view 的 Event
view.addEventListener( "GET_LIST" , onClick );
}
override public function onRemove():void{
//記得在 onRemove 中移除監聽
view.removeEventListener( "GET_LIST" , onClick );
}
override public function handleNotification(notification:INotification):void
{
//listNotificationInterests 有列這邊才收的到喔!
switch(notification.getName()){
case ListProxy.LIST_CHANGED:
view.setList( notification.getBody());
}
}
private function onClick(event:Event):void{
//假使已經做了一個 Command 來處理 ListProxy request...
//facade.registerCommand( "GET_DATA_LIST", ListProxyCommand );
sendNotification( "GET_DATA_LIST" );
}
//給自己看的,重點還是轉換物件型態
private function get view():UIComponent {
return viewComponent as UIComponent;
}
}
}


當然,要先註冊 ListMediator:

//listViewComponent 為畫面中某個 ViewComponent
facade.registerMediator( new ListMediator("ListMediator", listViewComponent );

Mediator 跟 ViewComponent 並不是只有一對一,也是可以控制多個 ViewComponent 喔!完全是看你要如何組織而已。
在 team work 中,處理前端介面的工程師其實只需要會串接 Mediator 即可,可以不用真正的熟悉整個 PureMVC 的運作,這樣分工起來簡單很多。

可喜可賀!!!恭喜大家撐到現在,主要的 class 都講完了!這份教學就是要配合實作才會更容易理解,就請各位多看一些範例吧!多練習才是王道啊!

相關連結:
PureMVC 官網上範例
[Flex] pureMVC Standard 練習筆記
[Flex] pureMVC 練習筆記啪兔
[ AS3] 簡單講客製 Event
[Flex] pureMVC and Utility - StateMachine
[Flex] pureMVC and Utility - StateMachine(2)
[AS3]PureMVC Utility - AsyncNotifier
Google PureMVC 任意門

Comments

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 資料做對應,如果要一筆一筆將資料鍵入,做久也是會膩的,所以花了點時間將這個工具做出來...