個人博客原文地址: http://www.qinblog.net/Articl...
最近看了阮一峯老師的博文 MVC,MVP 和 MVVM 的圖示,不少人對MVC圖示中的M和V之間的通訊產生了疑問,那張圖是這樣的:php
View 傳送指令到 Controller【1】
Controller 完成業務邏輯後,要求 Model 改變狀態【1】
Model 將新的數據發送到 View,用戶獲得反饋 【1】
問題來了。
What?Model將新的數據發送到View?不是V->C->M -> C -> V嗎?html
我可能用了假的MVC。java
固然,產生疑問的必定是寫web應用的童鞋。web
MVC是什麼:一種設計模式?一種解決方案?...balabala設計模式
每一個學習過web框架的朋友確定會思考到這個問題瀏覽器
wiki是這麼解釋的:MVC_wiki(這個詞條在wiki上其實也是頗有爭議的)ruby
固然後來MVC也有不少變體: MVP,MVVM...服務器
MVC能作什麼呢:業務邏輯和表現分離,數據展現相分離...balabalamvc
我是分割線app
MVC設計模式最初由Trygve Reenskaug這位挪威計算機科學家在70年代提出並應用在Xerox PARC的smalltalk系統上,成功的將數據模型從系統內容中分離出來。MVC設計模式廣泛的應用在GUI應用程序上。
傳統的MVC圖示:
沒錯,view的更新是靠model的。
例如Android的MVC框架,model只要發現自已屬性有改變,就會發出事件廣播,通知全部監聽自已此屬性的view,view收到通知後,會update自已。View只管接收改變的通知,controller只管改變model,model只管發出通知,這是非web應用的MVC框架常見的方案。【2】
其後這種設計模式被web開發者應用,成功的登錄web領域。而在web領域上的問題就是,客戶端是瀏覽器,view是HTML頁面,應用層的協議是HTTP,而HTTP是一個無狀態的協議,這種無狀態行爲使得model很難將更改通知view。在Web上,爲了發現對應用程序狀態的修改,瀏覽器必須從新查詢服務器。
90年代sun公司爲java web 應用推出了一個設計模式 JSP model 2 architecture,既MVC2(MVC Model 2),它解決了全站JSP的Model 1模式下代碼混亂的問題,並採用了MVC的設計思想。
同時,MVC 2的設計模式下:
View接受用戶輸入,並傳遞到Controller.【3】
Controller統一進行處理命令,交由Model處理具體的業務.【3】
通過處理Model更新後,Controller會選一個View並把Model內容傳遞給它.【3】
MVC2圖示
在MVC 2模式下,Controller「掌管大權」,處理起了model和view之間的聯繫,model和view變得互相不可見,沒法單獨通訊了(固然這不是結果而是緣由)。同時MVC 2也更好的解決了MVC在Web上的應用。
固然這裏不是指most valuable player啦(笑
MVC 2更像是Taligent公司90年代提出的Model–view–presenter設計模式,既MVP,固然,MVP強調了presenter和model、view的雙向通訊。【6】
MVP設計模式:
舉個栗子
CodeIgniter的MVC:
Ruby on rails的MVC:
是否是很熟悉呢?
一樣,sitepoint上的一篇關於ROR和MVC的文章Getting Started with MVC很好的闡述了Ruby on rails和MVC設計模式的聯繫:
History
In order to understand the MVC framework in depth, we will explore its history first. The Model-View-Controller pattern was formulated in the 1970s by Trygve Reenskaug as part of a smalltalk system being developed at Xerox PARC. This was long before the World Wide Web saga and even before computers became personal. The smalltalk system with MVC design pattern focused on separation of concern, a design principle on which all modern web frameworks base their prioritization. They successfully separated the data model (the data processing part) from the content and content from presentation. They also implemented an intermediate driving force which was the first-in-command. Although it had some downsides, it attracted web developers, resulting in many early web frameworks using the MVC design pattern (and obviously evolving into a de facto standard for building Web applications).【7】
So, is Rails a MVC framework?
Theoretically, Rails doesn’t adhere to MVC standards. Classic MVC had models that can notify views about the changes directly. But, in Rails, model data gets sent to the views via the controller, and it is the controller that returns the HTML output back to the browser. This closely matches the Model2 design pattern initially developed by Sun Microsystems in the late 1990s for designing Java Web applications. But this wouldn’t bother Rails developers, as long as they have a delicious cup of hot coffee while busy crunching code. :)【7】
博主翻譯:
爲了深刻理解MVC框架,先介紹一下MVC的歷史。模型-視圖-控制器(model-view-controller)模式是由Trygve Reenskaug在1970年代做爲正在開發的Xerox PARC smalltalk系統的一部分而制定的。這遠遠早於萬維網傳奇的產生,甚至遠早於我的電腦的產生。按照MVC設計模式的smalltalk系統關注關注點分離(separation of concern),一種全部現代Web框架都以其優先級爲基礎的設計原則。這個設計模式成功地將數據模型(數據處理部分)與演示內容從應用中分離出來,同時也首次實現了一箇中間驅動層(這裏指Cotroller)。雖然這個設計模式有一些缺點,可是瑕不掩瑜,很快的吸引了web開發者,使得不少早期的web框架使用了MVC設計模式(而且明顯的演變爲web應用搭建的事實標準)。
理論上來講,Rails不遵照MVC標準,傳統的MVC有狀態改變時能夠通知view的model, 可是在Rails中,模型數據經過控制器發送到視圖,而控制器將HTML輸出返回給瀏覽器,這與Sun公司最初於20世紀90年代開發的用於設計Java Web應用程序的Model2設計模式十分相似。不過這些並不會對Rails的開發者形成困擾,就像他們更但願在忙碌碼代碼時能有一杯好喝的熱咖啡同樣 :)
【1】MVC,MVP 和 MVVM 的圖示
【2】Android 設計模式之MVC模式
【3】MVC模式及MVC1和MVC2模式的區別
【4】Model-View-Controller
【5】JSP model 2 architecture
【6】Does PHP supports MVP pattern?
【7】What is MVC in Ruby on Rails?