MVC設計模式讀書筆記

 這個筆記來自《Advanced ActionScript 3 with Design Patterns》一書,是去年11月份讀的。幸好當時做了一些筆記。不然現在MVC章節的所有部分都要重新讀一遍了。雖然很多高手都覺得設計模式沒有什麼用處,但是MVC這一模式還是很實用的。

BTW,抵觸設計模式的人,好像並不抵觸MVC。大笑

  包包

如果要做蘋果的app,這個也應該是要掌握的。因爲我看斯坦福的apple教程裏面,第一個就是對MVC的解釋。如果要做WebGame客戶端,這個也應該是要掌握的。因爲我經歷的項目就是MVC架構的(每一個具體的子系統是一個MVC)。這本書對MVC的解釋非常到位,看了之後能夠讓你徹底搞清楚MVC到底是怎麼回事。

 

MVC Design(Architectural) Pattern 

括號中的字也表明,MVC也可以作爲客戶端的架構指導。

The basic principle of this pattern is to distinguish between the data and the presentation of that data.

  包包 
1,Understanding MVC Elements

The MVC pattern is composed of three subsystems as indicated by the name: the model, the view, and the controller. 
(1)Model : The defining aspects of the model are that it acts as a storehouse for data and that it exists independently of the view and the controller. The model should never have a reference to the view or the controller. This is absolutely essential to the functioning of the MVC pattern because the model's independence is what creates the flexibility in the MVC pattern.

(2)View : The view is the visual display portion of the user interface subsystem. The view uses the data from the model to draw itself. The key to understanding the view is to understand that it consists only of the visual elements and the logic necessary to read the model data and use it as required by the user interface.

(3)Controller : The controller is the subsystem that is responsible for taking input (user input, for example) and updating the model and view as necessary. 


2,The Relationships between Elements
(1)Model必須保持與View、Controller的獨立,也就是說Model不知道其他Elements的存在。A model can broadcast messages when the data changes. 但是Model在廣播消息的時候並不知道誰在偵聽。

(2)The view always knows about the model. The view interacts with the model in two ways: It listens for update messages, and it reads from the model. The view never writes to the model. Every view keeps a reference to its model. Because a view knows about its model but a model doesn't know about a view, a single model can act as the model for many views.

(3)The controller also knows about the model. The controller is responsible for updating the model when necessary based on user input or system events.

(4)The relationship between the controller and the view is very tightly coupled. Although it is possible to have a controller that uses several views, it is far more common that the relationship between view and controller is one-to-one. The view contains all the user interface elements through which the user interacts. Yet the controller is the element that responds to user input.(這裏說View包括所有的用戶界面,但是書中的例子,View只包括Model數據對應的界面,而與用戶交互的界面比如文本框、按鈕都是Controller提供的) In many, if not most, ActionScript applications, the view and the controller are one class. This variation of the MVC pattern is often called a Document View implementation of MVC.

(5)The most important key to the MVC pattern is that the model must be an independent object that does not have a reference to the view or controller. The view updates and redraws itself based on changes to the model.           包包

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