Skip to main content

PureMVC 我也會 [2]

PureMVC Gestalt

• Model and Proxy
• View and Mediator
• Controller and Commands
• Facade and Core
• Observers and Notification


Facade
  1. 簡化一切,讓你只需要對一個窗口
  2. Standard version 的 Facade 是個單例
  3. Multi-core 版本是由單例的 Facade bus(Array) 控制
  4. Model, View and Controller 三個單例的總代理
  5. Commands, Mediators and Proxy 的註冊、取用與移除都是在這裡
  6. 反正就是想要操作其他 class 時,可以先參考 facade 一下...
  7. 開始寫 PureMVC 專案後,你會發現它無所不在,甩都甩不掉...

每個專案的 ApplicationFacade 都長得很像(當然命名可以修改,內容不太需要變動)。如果你有自己特有的 Singleton Class 寫法,請自行修改。懶得改的人就直接用它官網上範例的寫法即可,其實乖乖的用也不會變壞到哪去啦!
以下為標準的 ApplicationFacade 寫法:
PS:範例 code 皆使用 Standard 版本 + Flex SDK 3.x or 4.x 來呈現,也許不是完整的寫法,請勿真的照抄後執行

package com.mvc
{
import org.puremvc.as3.patterns.facade.Facade;
import org.puremvc.as3.interfaces.IFacade;
import mx.core.Application;
import com.mvc.controls.StartupCommand;

public class ApplicationFacade extends Facade implements IFacade
{
public static const STARTUP:String = 'startup';
public function ApplicationFacade()
{
super();
}
public static function getInstance() : ApplicationFacade
{
if ( instance == null ) instance = new ApplicationFacade( );
return instance as ApplicationFacade;
}

override protected function initializeController( ) : void
{
super.initializeController();
registerCommand( STARTUP , StartupCommand );
}

public function startup( app:Application ) : void
{
sendNotification( STARTUP, app );
}
}
}


初始化 Facade

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
width="220" height="200" layout="absolute"
applicationComplete="ApplicationFacade.getInstance().startup( this )" >

<mx:List id="list"/>
</mx:Application>



ApplicationFacade.getInstance().startup( this ) // 就是這句...


ApplicationFacade 先繼承 Facade, 做的事情就是提供一個唯一的實體,再來註冊 string key "STARTUP" 的 Command (當然這個 StartupCommand 接下來會寫出來 ),接著就是發送通知(sendNotification) "STARTUP" ,順便將傳入的 app:Application 送出去...end... 真簡單對吧?

在 Proxy, Command and Mediator 要取用 Facade 怎麼辦?難道用 var facade:ApplicationFacade = ApplicationFacade.getInstance(); 來引用嗎?當然不可能!就說你甩不掉它了,因為這基本上常用到的 Class 都會帶著一個名為 「facade」 的屬性,直接用就可以囉!

其實你要註冊一堆相關 Proxy, Command, Mediator 都可以直接寫在 ApplicationFacade,為什麼它還要分離出去將初始化寫在 StartupCommand 內?這就是程式的藝術了。與其專案開發時增增減減不斷的改寫這個檔案, 到不如快快樂樂的用不同的 Command 來處理初始豈不是更乾淨?尤其專案通常是多人開發,人多手雜蟲蟲翩飛的道理不懂嗎?XD

Facade 可以用的 function 簡單到翻臉,國中英文有學好的應該都沒問題。
PureMVC AS3 API Doc - Facade
常用還是這幾個 register(註冊), remove(刪除), retrieve(取回)has(有沒有) 系列 function。

Facade 就先到此為止囉!

Comments

Popular posts from this blog

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 的時候一樣有幾個重點方...