Puremvc

Puremvc

PureMVC健壯、易擴展、易維護
Many so-called Model-View-Controller frameworks today seem to include everything but the kitchen sink. We decided that basic separation of responsibilities according to the MVC meta-pattern could be expressed with a handful of actors. We gave them a built-in means to communicate with each other which reinforces the classic MVC collaboration patterns, and did so with the simplest possible language features. That's why it's been ported to so many languages from the original ActionScript. It just works, with no "magic happens here" bits.

PureMVC優缺點:

  • 1.利用中介者,代理者,命令實現解耦,使得Model、View、Controller之間耦合性降低,提升了部分代碼的重用
  • 2.View界面可以實現重用
  • 3.Model數據可以實現重用
  • 3.代碼冗餘量大,對於簡單的功能都得創建View、Mediator、Command、Facade,Proxy,Model腳本
  • 4.操作過程比較繁瑣的流程,Mediator中的代碼會顯得流程較爲複雜難懂,除非你很熟悉PureMVC執行原理

PureMVC特點:

  • 1.通知的傳遞都要經過裝箱和拆箱的操作
  • 2.命令/通知是以觀察者模式實現,命令/通知在觀察者中利用反射獲取方法並執行
  • 3.沒有Service(可按照MVC的構造,自行添加與網絡通訊的這個模塊)
  • 4.數據通過通知傳遞,SendNotification只有一個object類型參數,會感覺數據傳輸受限,可以將數據組合成一個類型/結構傳遞,或者是爲Notification再拓展一個參數。

一、結構及示例

核心結構
核心結構

1、PureMVC結構

  • PureMVC把程序分爲低耦合的三層:Model、View和Controller。
  • ureMVC實現的經典MVC元設計模式中,這三部分由三個單例模式類管理,分別是Model、View和Controller。三者合稱爲核心層或核心角色。
    -PureMVC中還有另外一個單例模式類——Facade,Facade提供了與核心層通信的唯一接口,以簡化開發複雜度。

2、層級關係

三聯
三聯

PureMVC使用了觀察者模式,所以各層之間能以一種松耦合的方式通信,並且與平臺無關。

2.1、Model

數據層

  1. public class MyData 

  2. public int DataValue { get; set; } 

  3.  

2.2、Proxy

Proxy負責操作具體的數據模型,這樣保住了Model層的可移植性,Model只需維持自己的數據結構就好。

  1. using PureMVC.Patterns; 
  2.  
  3. /// <summary> 
  4. /// 通過proxy代理來進行對數據model的操作 
  5. /// </summary> 
  6. public class MydataProxy : Proxy 

  7. public const string proxname = "MyData01"
  8.  
  9. public MyData MyData; 
  10. public MydataProxy() : base(proxname) 

  11. MyData = new MyData();  

  12.  
  13.  
  14. public void AddValue() 

  15. MyData.DataValue++; 
  16.  
  17. //Facade 和Proxy只能發送Notification,Mediators既可以發送也可以接收Notification,Notification被映射到 Command, 
  18. //同時Command也可以發送Notification。這是一種“發佈/訂閱”機制,所有的觀察者都可以收到相同的通知。 
  19. //例如多個書刊訂閱者可以訂閱同一份雜誌,當雜誌有新刊出版時,所有的訂閱者都會被通知。 
  20. SendNotification("msg_add",MyData); 

  21.  
  22. public void SubValue() 

  23. MyData.DataValue--; 
  24.  
  25. SendNotification("msg_add", MyData); 

  26.  

2.3、Mediator

Mediator中介由,Mediator對象來操作具體的視圖組件,包括:添加事件監聽器,發送或接收Notification ,直接改變視圖組件的狀態。
這樣做實現了把視圖和控制它的邏輯分離開來。

  1. using System.Collections.Generic; 
  2. using PureMVC.Interfaces; 
  3. using PureMVC.Patterns; 
  4. using UnityEngine; 
  5. using UnityEngine.UI; 
  6.  
  7. public class MyMediator : Mediator 

  8. public const string MediatorName = "MyMeditor"
  9.  
  10. public Text textNum; 
  11. public Button AddButton; 
  12. public Button SubButton; 
  13.  
  14. public MyMediator(GameObject root):base(MediatorName) 

  15. //持有對視圖中對象的引用 
  16. textNum = root.transform.Find("TextValue").GetComponent<Text>(); 
  17. AddButton= root.transform.Find("BtnAdd").GetComponent<Button>(); 
  18. SubButton= root.transform.Find("BtnSub").GetComponent<Button>(); 
  19.  
  20.  
  21. // 視圖中的操作 
  22. AddButton.onClick.AddListener(Addbtn); 
  23. SubButton.onClick.AddListener(Subbtn); 

  24.  
  25. /// <summary> 
  26. /// 操作視圖 
  27. /// </summary> 
  28. /// <param name="myData">數據對象</param> 
  29. private void DisplayFun(MyData myData) 

  30. textNum.text = myData.DataValue.ToString(); 

  31.  
  32. /// <summary> 
  33. /// 與command層通信 
  34. /// </summary> 
  35. private void Subbtn() 

  36. SendNotification("cmd_sub"); 

  37.  
  38. /// <summary> 
  39. /// 與command層通信 
  40. /// </summary> 
  41. private void Addbtn() 

  42. SendNotification("cmd_add"); 

  43.  
  44. /// <summary> 
  45. ///定義自己需要關心的消息 
  46. /// </summary> 
  47. /// <returns></returns> 
  48. public override IList<string> ListNotificationInterests() 

  49. List<string> list=new List<string>() 

  50. "msg_add","msg_sub" 
  51. }; 
  52. return list; 

  53.  
  54. /// <summary> 
  55. /// 處理消息 
  56. /// </summary> 
  57. /// <param name="notification"></param> 
  58. public override void HandleNotification(INotification notification) 

  59. switch (notification.Name) 

  60. case "msg_add"
  61. case "msg_sub"
  62. DisplayFun(notification.Body as MyData);  
  63. break
  64. default
  65. break

  66. }  

  67.  

2.4、Command

Command命令層,Command可以獲取Proxy對象並與之交互,發送Notification,執行其他的Command。經常用於複雜的或系統範圍的操作,如應用程序的“啓動”和“關閉”。應用程序的業務邏輯應該在這裏實現。

  1. using PureMVC.Interfaces; 
  2. using PureMVC.Patterns; 
  3.  
  4. public class MyCommandAdd : SimpleCommand 

  5. public override void Execute(INotification notification) 

  6. //直接調度Proxy處理具體邏輯 
  7. MydataProxy mydataProxy = Facade.RetrieveProxy("MyData01") as MydataProxy; 
  8.  
  9. mydataProxy.AddValue(); 


  10.  
  11. public class MyCommandSub : SimpleCommand 

  12. public override void Execute(INotification notification) 

  13.  
  14. MydataProxy mydataProxy = Facade.RetrieveProxy("MyData01") as MydataProxy; 
  15.  
  16. mydataProxy.SubValue(); 


2.5、MyFacade

Facade類應用單例模式,它負責初始化核心層(Model,View和Controller),並能訪問它們的Public方法。
這樣,在實際的應用中,你只需繼承Facade類創建一個具體的Facade類就可以實現整個MVC模式,並不需要在代碼中導入編寫Model,View和Controller類。
Proxy、Mediator和Command就可以通過創建的Façade類來相互訪問通信。
一般地,實際的應用程序都有一個Facade子類,這個Facade類對象負責初始化Controller(控制器),建立Command與Notification名之間的映射,並執行一個Command註冊所有的Model和View。

  1. using PureMVC.Patterns; 
  2. using UnityEngine; 
  3.  
  4. public class MyFacade : Facade 

  5. public MyFacade(GameObject root) : base() 

  6. //註冊命令,實現消息與命令之間的綁定 
  7. RegisterCommand("cmd_add",typeof(MyCommandAdd)); 
  8. RegisterCommand("cmd_sub",typeof(MyCommandSub)); 
  9.  
  10. RegisterMediator(new MyMediator(root)); 
  11. RegisterProxy(new MydataProxy()); 


2.6 初始化Facade

注意:除了頂層的Application,其他視圖組件都不用(不應該)和Façade交互。

頂層的Application構建視圖結構、初始化Façade,然後“啓動”整個PureMVC機制。

  1. using System.Collections; 
  2. using System.Collections.Generic; 
  3. using UnityEngine; 
  4.  
  5. public class GameManager : MonoBehaviour 

  6. public GameObject root; 
  7. // Start is called before the first frame update 
  8. void Start() 

  9. //初始化MyFacade啓動PureMVC 
  10. MyFacade myFacade = new MyFacade(root); 

  11.  

它山之石可以攻玉

[Unity]PureMVC框架解讀(上)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章