PDMvcFramework
MVC介紹
MVC框架
將應用程序分爲Model、View、Controller三個大模塊,Model主要負責應用程序的數據持有,View模塊負責業務UI展示,Controller模塊負責業務邏輯處理。
MVC框架實現
使用管理類統一管理註冊Controller,Controller管理自己的View與Model,在Controller與Controller之間實現事件機制爲模塊與模塊之間通信做橋樑,模塊內部Controller可以控制Model與View的邏輯,View響應Controller操作並顯示UI,Model由Controller控制,存儲數據。
PureMvc框架
PureMvc
宏觀上將應用程序分爲Model、Controller以及View三大模塊,在模塊內以代理的方式將各個模塊細分,並以Facade(單例)作爲各個細分模塊(Mediator、Proxy以及Command)的管理:
- Model以Proxy作爲各個模塊數據存儲;
- Meditor代理View組件的行爲,發生行爲之後發送Notification;
- Command作爲Mediator行爲的執行者,將Mediator發送的Notification映射爲一個個可執行的Command;
PureMvc通信
- 用戶操作View,調用Mediator函數
- Mediator將用戶操作以及數據封裝,併發送Notification
- Facade將發送的Notification派發到對應處理的Command
- Command處理完操作,存儲數據(如果有的話),然後發送Notification通知Mediator
- Mediator收到Notification之後顯示View
模塊以及細分
View與Mediator
Mediator將View細分模塊之後,各個View模塊的“UI行爲”操作由Mediator代理完成,Mediator是View行爲與Controller(大模塊)的中介,有View統一管理。
Model與Proxy
Proxy將Model層細分爲一個個代理(Proxy),將程序的數據分組,並由Model統一管理。
Controller與Command
Controller統一管理各個Command操作,並且在收到Notification的時候將對應的Notification映射到Command並執行。
Facade與MVC
MVC將三層註冊爲一個個的單例之後由Facade統一管理MVC三層,Facade作爲MVC三層的“門面”,爲MVC的子模塊提供功能。
觀察者模式
Notification
Notification爲觀察者模式提供數據行爲支持,是觀察者模式傳遞數據的中介,接口如下:
函數 | 參數 | 返回值 | 說明 |
---|---|---|---|
getNotificationName | void | QString | 獲取該notification的標識(名字) |
getBody | void | Qvaraient | 返回該notification攜帶的數據 |
Observer
Observer作爲notification的監聽者,是notification行爲的執行人,Observer將會被註冊到ObserverFacade(HashMap),ObserverFacade在收到Notification的時候從表中取出需要執行的Observer讓其執行:
函數 | 參數 | 返回值 | 說明 |
---|---|---|---|
handleNotification | INotification | void | notification調用函數 |
Notifier
Notification 消息發送者,消息發送者在調用Notification之後,將消息映射到Observer表之後,找出對應的執行者執行Notification:
函數 | 參數 | 返回值 | 說明 |
---|---|---|---|
sendNotification | Notification | void | notification發送函數 |
Command模式
Command模式映射一個個Notification爲具體的執行操作,將Mediator發出的Notification轉換爲實際的業務邏輯: