標籤:style blog http color os io strong ar for
MVC
MVC概述:
Model–view–controller (MVC) is a software architectural pattern for implementing user interfaces.
MVC模式(Model-View-Controller)是軟體工程中的一種軟體架構模式,把軟體系統分為三個基本部分:模型(Model)、視圖(View)和控制器(Controller)。
MVC模式的目的是實現一種動態程式設計,使後續對程式的修改和擴充簡化,並且使程式某一部分的重複利用成為可能。
- (控制器Controller)- 負責轉寄請求,對請求進行處理。
The controller, accepts input and converts it to commands for the model or view.
- (視圖View) - 介面設計人員進行圖形介面設計。
A view can be any output representation of information, such as a chart or a diagram; multiple views of the same information are possible, such as a bar chart for management and a tabular view for accountants.
- (模型Model) - 程式員編寫程式應有的功能(實現演算法等等)、資料庫專家進行資料管理和資料庫設計(可以實現具體的功能)。
The model, captures the application‘s behavior in terms of its problem domain, independent of the user interface. The model directly manages the application‘s data, logic and rules.
MVC組件中的互動圖示
詳細解析各組成部分:
- 模型(Model) 用於封裝與應用程式的商務邏輯相關的資料以及對資料的處理方法。“模型”有對資料直接存取的權力,例如對資料庫的訪問。“模型”不依賴“視圖”和“控制器”,也就是說,模型不關心它會被如何顯示或是如何被操作。但是模型中資料的變化一般會通過一種重新整理機制被公布。為了實現這種機制,那些用於監視此模型的視圖必須事先在此模型上註冊,從而,視圖可以瞭解在資料模型上發生的改變。(比較:觀察者模式(軟體設計模式))
- A model notifies its associated views and controllers when there has been a change in its state. This notification allows the views to produce updated output, and the controllers to change the available set of commands. In some cases an MVC implementation might instead be "passive," so that other components must poll the model for updates rather than being notified.
- 視圖(View)能夠實現資料有目的的顯示(理論上,這不是必需的)。在視圖中一般沒有程式上的邏輯。為了實現視圖上的重新整理功能,視圖需要訪問它監視的資料模型(Model),因此應該事先在被它監視的資料那裡註冊。
- A view requests information from the model that it uses to generate an output representation to the user.
- 控制器(Controller)起到不同層面間的組織作用,用於控制應用程式的流程。它處理事件並作出響應。“事件”包括使用者的行為和資料模型上的改變。
- A controller can send commands to the model to update the model‘s state (e.g., editing a document). It can also send commands to its associated view to change the view‘s presentation of the model (e.g., by scrolling through a document).
百科MVC資料流圖示
MVC組件類型關係與功能圖示
優點:
1、多個視圖能共用一個模型。如:要求Web網站同時能提供Internet介面和WAP介面。在MVC設計模式中,模型響應使用者請求並返迴響應資料,視圖負責格式化資料並把它們呈現給使用者,商務邏輯和展示層分離,同一個模型可以被不同的視圖重用,所以大大提高了代碼的可重用性。
2、控制器是自包含(self-contained)指高獨立內聚的物件,與模型和視圖保持相對獨立,所以可以方便的改變應用程式的資料層和商務規則。
3、控制器提高了應用程式的靈活性和可配置性。控制器可以用來串連不同的模型和視圖去完成使用者的需求,也可以構造應用程式提供強有力的手段。給定一些可重用的模型和視圖,控制器可以根據使用者的需求選擇適當的模型進行處理,然後選擇適當的的視圖將處理結果顯示給使用者。
MVC架構模式概述