首先看看兩者的區別:
MVC:Mode、View、Controller
PureMVC:MVC、Proxy、Mediator、Command、Facade
接着小心推敲各自的職責:
Model、Proxy:
Model保存對Proxy對象的引用,Proxy負責操作數據模型,與遠程服務通信存取數據。(這樣保證了Model層的可移植性)
View、Mediator:
View保存對Mediator對象的引用。由Mediator對象來操作具體的視圖組件(例如DataGrid組件),包括:添加監聽、發送或接收Notification,改變視圖組件的狀態。
Controller、Command:
Controller保存所有Command的映射。Command類是無狀態的,只在需要時才被創建。
Command可以獲取Proxy對象並與之交互,發送Notification,執行其他的Command。例如應用程序的“啓動”和“關閉”。
Facade:
單例,負責初始化核心層(Model、View和Controller),並能訪問他們的Public方法。
(1)、負責初始化Controller,建立Command與Notification之間的映射。
(2)、執行一個Command,註冊所有的Mode和View
(3)、將Mode、View和Controller對象的引用保存在自己的成員變量。
通信方式:
PureMVC通信:
不採用flash的事件響應,而是使用觀察者模式來通信。
實際被我們使用的:
Notification:
Facade保存了Command與Notification之間的映射。當Notification被髮出時,對應的Command會自動地由Controller執行。
Mediator:(發送、聲明、接收Notification)
當用View註冊Mediator時,Mediator的listNotifications方法會被調用,以數組形式返回該Mediator對象所關心的所有Notification。
之後,當系統其它角色發出同名的Notification時,關係這個通知的Mediator都會調用handleNotification方法,並將Notification以參數傳遞到方法。
Proxy:(發送,但不接收Notification)
例如,Proxy從服務器收到數據時,發送Notification到系統。當Proxy的數據被更新時,發送Notification到系統。
View和Controller監聽Proxy發送的Notification。用戶通過界面(view)與數據(Proxy)交互。
對外公佈操作數據對象的API。封裝了所有對數據模型的操作。
有這些命令:
SimpleCommand:
只有一個execute方法,僅接受一個Inotification實例作爲參數。
MacroCommand:
可以執行多個Command。每個執行都會創建一個Command對象並傳參一個對源Notification的引用。
它在構造方法中調用自身的initializeMacroCommand方法。只需要重寫這個方法,調用addSubCommand添加子Command。
也可以任意組合Simple和Macro成爲一個新的Command。