http://www.gameres.com/thread_484046_1_1.htmlhtml
MVC從施樂帕克實驗室提出至今,已經應用到各類應用開發領域中:Web App能夠用MVC,iOS/Android/Windows客戶端應用也用MVC,Web 前端也在用MVC,等等;這些幾乎涵蓋了咱們常見的開發領域,因此MVC其實已經超越了他本來最初的設計,基於全部涉及展現的應用都 能套上MVC,只不過不一樣的平臺在設計上略有差異。而MVP和MVVM,也不過是MVC的衍生變種,除這二者以外,還有咱們沒怎麼見過的HMVC 、MVA等。
4.2 Model Layer
在討論MVP和MVVM以前,我想先明確一個常常被誤解的概念:Model。因爲Model這個詞太通用化,如數據Model,數據庫Model,這就致使 了Model這一律念理解差別化,簡單的說,就是被玩壞。拋開其餘,咱們來看看常見的定義:
Wikipedia的定義:
前端
- The central component of MVC, the model, captures the behavior of the application in terms of its problem domain, independent of the user interface.[11] The model directly manages the data, logic and rules of the application.
複製代碼
MSDN(https://msdn.microsoft.com/en-us/library/ff649643.aspx)中的定義:
數據庫
- Model. The model manages the behavior and data of the application domain, responds to requests for information about its state (usually from the view), and responds to instructions to change state (usually from the controller).
複製代碼
上面兩個定義基本一致:Model,管理應用的行爲和數據。
再來看看Apple官方文檔Model-View-Controller的定義:
設計模式
- Model Objects
- Model objects encapsulate the data specific to an application and define the logic and computation that manipulate and process that data. For example, a model object might represent a character in a game or a contact in an address book. A model object can have to-one and to-many relationships with other model objects, and so sometimes the model layer of an application effectively is one or more object graphs. Much of the data that is part of the persistent state of the application (whether that persistent state is stored in files or databases) should reside in the model objects after the data is loaded into the application. Because model objects represent knowledge and expertise related to a specific problem domain, they can be reused in similar problem domains. Ideally, a model object should have no explicit connection to the view objects that present its data and allow users to edit that data—it should not be concerned with user-interface and presentation issues.
- Communication: User actions in the view layer that create or modify data are communicated through a controller object and result in the creation or updating of a model object. When a model object changes (for example, new data is received over a network connection), it notifies a controller object, which updates the appropriate view objects.
複製代碼
雖然Apple的官方文檔是定義Model Objects,但它的含義仍是封裝數據以及管理數據相關的邏輯計算;
因此這裏須要明確的一個概念是:在MVC的設計模式中,Model是一個Layer,而不僅是一個數據模型(Data Model)類。整體來講, Model Layer 包含了數據模型,以及管理這些數據相關的邏輯計算,如本地數據變化、數據緩存、從網絡請求數據等業務邏輯。關於這 個問題,還能夠參考這篇文章:《iOS應用架構談 view層的組織和調用方案》。但有一點須要說明:該文章更傾向於從Model Object上 思考Model的定義,由於裏面的關於Model的示例是從數據模型中擴展出業務接口;而本人則更傾向於從Model Layer來思考Model,即 Model並不限於數據模型,能夠是數據管理類(各類Manager)、請求隊列管理等等。緩存