標籤雲

Android (59) ActionScript (52) PHP (14) JavaScript (11) 設計模式 (10) CSS與Html (8) Flex (7) Material Design (6) frameworks (5) 工具 (5) 串流影音 (4) 通用 (4) DB (3) FlashRemoting (3) Java (3) SQL (3) Mac 操作 (2) OpenGL ES (2) PureMVC (2) React Native (2) jQuery (2) AOSP (1) Gradle (1) XML (1) 軟體設定 (1)

搜尋此網誌

顯示具有 PureMVC 標籤的文章。 顯示所有文章
顯示具有 PureMVC 標籤的文章。 顯示所有文章

2011/02/10

PureMVC 入門筆記-2

接著來看看 Notification

Notification

Package:org.puremvc.as3.patterns.observer
實作 INotification,與 Flash 的 Event 機制不同的是
Event 機制遵循的是 責任鍊(Chain of Responsibility) 模式,事件會跟著 displayObject 的 parent 一直上浮
而 Notification 則是遵照發布/訂閱模式,訂閱者只接收感興趣的消息,與接收者之間並無 parent/child 關係
而 Notification 並不是用來取代 Event 機制的,它們需要相互合作使用

主要 public 方法有:
//建構子
Notification(name:String, body:Object = null, type:String = null)

getBody():Object
getName():String
getType():String

setBody(body:Object):void
setType(type:String):void

toString():String

由於 Proxy, Mediator, Command 都繼承自 Notifier 並實作 INotifier
所以有一些共通的屬性方法
//連到 Facade 實體的參照
facade : IFacade
initializeNotifier(key:String):void
sendNotification(notificationName:String, body:Object = null, type:String = null):void
這些就不一一列在下面了
而要注意的是 Mediator/View 之間的關係不可避免的是緊耦合,Proxy/Data 也是一樣。但這樣可以讓他們與其他程式碼變成松耦合

Proxy

Package:org.puremvc.as3.patterns.proxy
繼承 Notifier,實作 INotifier, IProxy
Proxy 的角色是被 Model 註冊的數據持有者,並用來實現 Domain Logic,其中 data 可以透過 getter 轉型為它真正的型別

Protected 屬性:
data : Object
proxyName : String

主要 public 方法有:
//建構子
Proxy(proxyName:String = null, data:Object = null)

setData(data:Object):void
getData():Object
getProxyName():String

Mediator

Package:org.puremvc.as3.patterns.mediator
繼承 Notifier,實作 INotifier, IMediator
Mediator 的工作是負責讓 view component 與系統其他部分進行溝通,其中 viewComponent 可以透過 getter 轉型為它真正的型別

Protected 屬性:
mediatorName : String
//用來存放視覺組件的變數
viewComponent : Object

主要 public 方法有:
//建構子
Mediator(mediatorName:String = null, viewComponent:Object = null)

//在這邊列出要 Mediator 感興趣的 INotification 的名稱,這裡有列的,handleNotification()才會收到
listNotificationInterests():Array
//Mediator需要監聽 Flash 裡的 Event (來自 view Component),也要監聽 Notification,其差別就在於使用 listNotificationInterests() 跟 handleNotification() 這兩個方法來管理 Notification
handleNotification(notification:INotification):void
//handleNotification 範例 CODE
override public function handleNotification(notification:INotification):void{
 //建議使用 switch/case 來處理 Notification
 switch(notification.getName()){
  //這裡處理的 Notification 如果太多(超過四五個),應把 Mediator 拆開
  case LoginProxy.LOGIN_FAILED:
   //..省略...
   break;
  case LoginProxy.LOGIN_SUCCESS:
   //..省略...
   break;
 }
}

//viewComponent 的取得與設定
setViewComponent(viewComponent:Object):void
getViewComponent():Object

getMediatorName():String

至於 Command,則分成 SimpleCommand 跟 MacroCommand 兩種
差別在於 MacroCommand 可以執行其他 ICommand
Command 的功能在於協調 Proxy、處理異常等等

SimpleCommand

Package org.puremvc.as3.patterns.command
繼承 Notifier,實作 INotifier, ICommand

繼承自 Notifier 的屬性與方法
//連到 Facade 實體的參照
facade : IFacade
sendNotification(notificationName:String, body:Object = null, type:String = null):void

自己的方法只有一個,就是
execute(notification:INotification):void
這個方法是給我們 override 用的,可以把要處理的事情寫進去

MacroCommand
Package org.puremvc.as3.patterns.command
繼承 Notifier,實作 INotifier, ICommand

除了跟 SimpleCommand 一樣擁有
facade : IFacade
execute(notification:INotification):void
sendNotification(notificationName:String, body:Object = null, type:String = null):void
之外

還有以下方法:
initializeMacroCommand():void
addSubCommand(commandClassRef:Class):void

addSubCommand(commandClassRef:Class) 可以為 Command 增加 SubCommand
( SubCommand 本身可以是 SimpleCommand 也可以是 MacroCommand)
在 execute() 時會遵循 First In/First Out (FIFO) 的順序

要注意的是 execute() 在這裡已經變成一個 final function
所以要改為 override 它的 initializeMacroCommand() 方法
initializeMacroCommand()的 override 應該要像這樣:
override protected function initializeMacroCommand():void{
 addSubCommand(me.myapp.controller.FirstCommand);
 addSubCommand(me.myapp.controller.SecondCommand);
 addSubCommand(me.myapp.controller.ThirdCommand);
}

以上,已經把這四個重要的類別掃過一遍了
再搭配高手們的範例來看應該就會對 PureMVC 有一些認識

PureMVC 入門筆記-1

雖然已經有很多先進寫過關於 PureMVC 的相關文章
但是我還是習慣自己整理一下筆記來釐清觀念

PureMVC 的目的就是實踐 MVC 設計模式,把程式分成Model、View、Controller 三個部份,當然裡面還實作了 GoF 設計模式:可復用物件導向軟體的基礎裡面的其他設計模式(代理模式, 命令模式, 觀察者模式, 外觀模式, 單例模式, 工廠方法模式, 中介者模式...等)

Model (負責轉發請求,對請求進行處理) --> Proxy
View (使用者介面) --> Mediator
Controller(資料管理或實作演算法) --> Command
Facade (實做外觀模式,使用單一類別作為MVC三者溝通之用)

而溝通則使用 Notification (實做觀查者模式),不使用 Flash 的 Event 機制,以便移植到其他語言
Notification 可以被用來觸發 Command 的執行
Facade, Proxy, Mediator, Command 大家都可以 sendNotification
而 Facade 跟 Proxy 不會接收 Notification

以下針對個別部份進行詳細說明:( Standard 版本)
Facade

Package:org.puremvc.as3.patterns.facade
是 IFacade 的單例實作
Facade 的功能有:
初始化 Model, View, Controller 的單例類別並提供所有方法
對主程式註冊 Command 的地方

主要 public 方法有:
getInstance():IFacade

//建立並發送 INotification.
sendNotification(notificationName:String, body:Object = null, type:String = null):void
//取得 Mediator
retrieveMediator(mediatorName:String):IMediator
//取得 Proxy
retrieveProxy(proxyName:String):IProxy

//註冊 Command, Mediator, Proxy
registerCommand(notificationName:String, commandClassRef:Class):void
registerMediator(mediator:IMediator):void
registerProxy(proxy:IProxy):void

//移除 Command, Mediator, Proxy
removeCommand(notificationName:String):void
removeMediator(mediatorName:String):IMediator
removeProxy(proxyName:String):IProxy

//檢查 Command, Mediator, Proxy 是否已註冊
hasCommand(notificationName:String):Boolean
hasMediator(mediatorName:String):Boolean
hasProxy(proxyName:String):Boolean

Protected 屬性及方法有:
instance : IFacade [static]
model : IModel
view : IView
controller : IController
//初始化的方法
initializeFacade():void
initializeModel():void
initializeView():void
initializeController():void

Facade 的範例 ApplicationFacade.as:
package com.me.myapp{
//import
import com.me.myapp.view.*;
import com.me.myapp.model.*;
import com.me.myapp.controller.*;

import org.puremvc.as3.interfaces.*;
import org.puremvc.as3..patterns.facade.*;

// 繼承 Façade, 實作 IFacade
public class ApplicationFacade extends Façade implements IFacade{
 // 為 Notification 定義常數
 public static const STARTUP:String = "startup";
 public static const LOGIN:String = "login";
 // 用以取得單一實體的工廠方法
 public static function getInstance() : ApplicationFacade{
  if ( instance == null ) instance = new ApplicationFacade();
  return instance as ApplicationFacade;
 }
 // 初始化 Controller,註冊 Command (建立 Notification 與 Command 的對應關係)
 override protected function initializeController( ):void{
  super.initializeController();
  registerCommand( STARTUP, StartupCommand );
  registerCommand( LOGIN, LoginCommand );
  registerCommand( LoginProxy.LOGIN_SUCCESS, GetPrefsCommand );
 }
 /** 建立一個 startup 方法把應用程式作為參數傳入
  * 並發出 Notification 將它送到已註冊的 StartupCommand
  */
 public function startup( app:MyApp ):void{
  sendNotification( STARTUP, app );
 }
}
}

整理到這邊好像篇幅差不多了
下一篇繼續來看 Proxy, Mediator, Command, 還有 Notification