Model-View-Controller
The Model-View-Controller (MVC) design pattern assigns objects in an application one of three roles: model, view, or controller. The pattern defines not only the roles objects play in the application, it defines the way objects communicate with each other. Each of the three types of objects is separated from the others by abstract boundaries and communicates with objects of the other types across those boundaries. The collection of objects of a certain MVC type in an application is sometimes referred to as a layer—for example, model layer.
Model-View-Controller(MVC)设计模式将一个程序中的对象分配为三个角色之一:model,view,或者controller。这个模式不仅定义了对象在程序中扮演的角色,还定义了对象彼此之间的通信方式。三种类型对象中的每一种都是通过抽象边界与其它对象分开,并且跨越这些边界与其它类型的对象通信。程序中某种MVC类型对象的集合有时候被称为层—例如,模型层。
理解:将一个程序中所有对象通过一种抽象的边界分为model,view,和controller三种类型,并且这三种类型是以指定的方式彼此通信的模式称为MVC模式。其中某种类型的对象的集合可以称为层。
MVC is central to a good design for a Cocoa application. The benefits of adopting this pattern are numerous. Many objects in these applications tend to be more reusable, and their interfaces tend to be better defined. Applications having an MVC design are also more easily extensible than other applications. Moreover, many Cocoa technologies and architectures are based on MVC and require that your custom objects play one of the MVC roles.
MVC是一个优秀Cocoa程序设计的核心。采用这种模式的好处有很多。这些程序中的很多对象更加可重用,而且这些程序的界面更好设计。拥有一个MVC架构的程序相对其它程序也更容易拓展。此外,很多Cocoa技术和架构都基于MVC,并且要求你的自定义对象扮演MVC中角色之一。
理解:MVC能使对象更容易重用,界面更容易设计。很多Cocoa技术和架构都基于MVC,使自定义对象不得不遵循MVC。
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.
模型封装了程序中特定的数据,并且定义了操作和处理这些数据的逻辑和运算。例如,一个模型对象可能表示游戏中的角色,或者电话簿中的联系人。一个模型与和其它模型之间可能有一对一或者一对多的关系,所以有时候一个程序中的模型层实际上是一个或者多个对象图。作为程序持久化状态(persistent state)的一部分的大量数据(不管那个持久化状态是存储在文件中还是数据库中),都应该在数据被加载进程序后,驻留在模型中。因为模型表示为与特定问题域(problem domain)相关的知识和技能(knowledge and expertise),它们可以在相似的问题域中被重用。理想情况下,一个模型对象应该与展示它的数据和允许用户编辑它的数据的视图没有显示连接—它不应该关注用户界面和显示问题。
理解:模型封装了数据和操作,处理数据的方法。模型之间关系可以是一对一和一对多,也就是模型嵌套一个或多个模型。处理类似问题的模型应该重用。理想情况下, 模型应该与界面和展示无关。
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.
视图层创建和修改数据是通过控制器对象通信,从而导致数据的创建和更新。当一个模型对象更改了(例如,接收到网络连接的新数据),模型通知控制器对象,从而更新对应的视图。
理解:这里容易理解错误,通过控制器对象通信,并不是通过控制器来修改。控制器只是起到一个传递作用。
_
View Objects
A view object is an object in an application that users can see. A view object knows how to draw itself and can respond to user actions. A major purpose of view objects is to display data from the application’s model objects and to enable the editing of that data. Despite this, view objects are typically decoupled from model objects in an MVC application.
Because you typically reuse and reconfigure them, view objects provide consistency between applications. Both the UIKit and AppKit frameworks provide collections of view classes, and Interface Builder offers dozens of view objects in its Library.
Communication: View objects learn about changes in model data through the application’s controller objects and communicate user-initiated changes—for example, text entered in a text field—through controller objects to an application’s model objects.
视图是程序中用户可以看到的对象。一个视图对象知道怎么渲染自己和相应用户动作。视图对象的主要目的是展示来自模型的数据和启用这些数据的编辑。尽管如此,视图对象在MVC程序中还是通常和模型对象分开。
因为你要重用和重新配置视图,视图对象提供程序之间固定的部分。UIKit和AppKit框架提供了一系列的视图类,并且界面编辑器(Interface Builder)在它的库中提供了许多视图对象。
讨论(communication):视图对象从控制器对象中知道模型的改变,并且传递用户发起的更改-例如,text field视图中输入了文本-通过控制器对象传递到模型对象。
理解:中这段中我们可以知道,之所以将视图和模型分开,是因为视图在应用程序之间可以复用。视图的主要功能就是显示来自模型的数据和启用模型的编辑。
Controller Objects
A controller object acts as an intermediary between one or more of an application’s view objects and one or more of its model objects. Controller objects are thus a conduit through which view objects learn about changes in model objects and vice versa. Controller objects can also perform setup and coordinating tasks for an application and manage the life cycles of other objects.
Communication: A controller object interprets user actions made in view objects and communicates new or changed data to the model layer. When model objects change, a controller object communicates that new model data to the view objects so that they can display it.
控制器对象在视图和模型之间扮演者中间人(intermediary)的角色。控制器对象就是一个视图来了解模型更改的管道(conduit),反过来也一样(vice versa)。控制器也执行一些设置和协调任务也可以管理其他对象的生命周期。
交流(communication):控制器解释(interpret)了在视图上发生的用户行为,并且传递新的或者修改过的数据到模型层。当模型更改了,控制器对象传递这个新模型数据到视图,使之能显示它。
理解:看到这里,有一句值得注意的话:communicates new or changed data to the model layer.也就是说数据在视图中修改完了, 再由控制器传递给模型。还有一句:When model objects change, a controller object communicates that new model data to view objects so that they can display it.当数据修改了,控制器把新的模型数据传到视图,让视图来显示它。也就是说数据是在模型中修改完了的。控制器只是一个传递作用。由此看出,可以在视图中修改数据。