Skip to main content

PureMVC 我也會 [3]

Notification & Observer
  • 簡單講就是 PureMVC 架構中的 Event 機制
  • 背後就是所謂的「Observer Design Pattern」
  • Notification 為訊息物件
  • Facade and Proxy 只能發送
  • Command and Mediator 可以接收與發送

Observer Design Pattern
「觀察者」這個設計模式被提出來一定有它的過人之處,寫程式講求的就是「別人家的事情管他去死,什麼渾水都要淌的話,怎麼被蟲咬死都不知道」。觀察者做的事情就是有興趣接收的消息,就自己乖乖訂閱,出了啥大事,就是用將訊息發出去,有訂閱的人自然會收到,收到訊息要做什麼反應當然就是自己的事情,最後收膩了還可以取消訂閱。在 PureMVC 中可以發送的 class 都可以用內建的 sendNotification() 發送訊息:

sendNotification( NotificationName:String , body:Object , type:String );
//body:Object 萬物皆物件啊,如果你還不知道能送什麼出去我也沒辦法了...


接收端會收到被打包成 notification:Notification 的物件,可以用其 getName(), getBody() and getType() 方法取得訊息的內容。

Command 中能接收到的訊息是通過 facade.registerCommand( NotificationName:String , command:Class ) 達到 mapping,而 Mediator 中要接收 Notification 要在其 listNotificationInterests 方法中列出有興趣 Notifications 清單。

Proxy 不能接收也有它的用意,Model 最大的功能就是提供資料,不需要理會外面的世界到底發生了什麼事情,如果能接收訊息的話,直接拿 Mediator 來用就好了,還需要使用 Proxy 嗎?

to be continued...

Comments

Popular posts from this blog

[書評] 拖延心理學:為什麼我老是愛拖延?是與生俱來的壞習慣,還是身不由己?

作者: Jane B. Burka & Lenora M . Yuen 推薦指數 ★★★★★ 有時候,只是想了解事情發生原因而不是尋求解法 在這邊不是要講這本書的內容,而是想聊它對我的影響。