MVC

MVC是一种高级模式,因为它关注应用程序的全局体系结构,并根据对象在应用程序中所扮演的一般角色对对象进行分类。它也是一个复合模式,因为其他包含了几个更基本的模式。(It is a high-level pattern in that it concerns itself with the global architecture of an application and classifies objects according to the general roles they play in an application. It is also a compound pattern in that it comprises several, more elemental patterns.)

MVC对象的角色和关系(Roles and Relationships of MVC Objects)

MVC设计模式认为有三种类型的对象:模型对象、视图对象和控制器对象。MVC模式定义了这些类型的对象在应用程序中所扮演的角色以及它们的通信线路。在设计应用程序时,一个主要步骤是为对象选择(或创建)属于这三组之一的自定义类。这三种类型的对象都通过抽象边界与其他对象隔开,并跨越这些边界与其他类型的对象通信。(The MVC design pattern considers there to be three types of objects: model objects, view objects, and controller objects. The MVC pattern defines the roles that these types of objects play in the application and their lines of communication. When designing an application, a major step is choosing—or creating custom classes for—objects that fall into one of these three groups. 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.)

模型对象(Model Objects)

理想情况下,模型对象与用于表示和编辑它的用户界面没有显式连接。例如,如果您有一个表示一个人的模型对象(假设您正在编写一个地址簿),那么您可能希望存储一个生日。这是存储在Person模型对象中的好东西。然而,存储日期格式字符串或关于该日期如何显示的其他信息可能最好放在其他地方。(Ideally, a model object has no explicit connection to the user interface used to present and edit it. For example, if you have a model object that represents a person (say you are writing an address book), you might want to store a birthdate. That’s a good thing to store in your Person model object. However, storing a date format string or other information on how that date is to be presented is probably better off somewhere else.)

视图对象(View Objects)

视图对象知道如何显示应用程序模型中的数据,并且可能允许用户编辑这些数据。视图不应该负责存储它所显示的数据。(当然,这并不意味着视图从未真正存储它所显示的数据。出于性能原因,视图可以缓存数据或执行类似的操作)。视图对象可以只负责显示模型对象的一部分,或者显示整个模型对象,甚至显示许多不同的模型对象。视图有许多不同的种类。(A view object knows how to display, and might allow users to edit, the data from the application’s model. The view should not be responsible for storing the data it is displaying. (This does not mean the view never actually stores data it’s displaying, of course. A view can cache data or do similar tricks for performance reasons). A view object can be in charge of displaying just one part of a model object, or a whole model object, or even many different model objects. Views come in many different varieties.)

控制器对象(Controller Objects)

控制器对象充当应用程序视图对象与其模型对象之间的中介。控制器通常负责确保视图能够访问它们需要显示的模型对象,并充当视图了解模型更改的通道。控制器对象还可以为应用程序执行设置和协调任务,并管理其他对象的生命周期。(A controller object acts as the intermediary between the application’s view objects and its model objects. Controllers are often in charge of making sure the views have access to the model objects they need to display and act as the conduit through which views learn about changes to the model. Controller objects can also perform set-up and coordinating tasks for an application and manage the life cycles of other objects.)

传统的MVC关系图

1-1
Apple的MVC关系图

图2

事实上,根据图7-1中的关系图构建一个工作应用程序是很有可能的。通过使用绑定技术,您可以轻松创建一个Cocoa MVC应用程序,其视图直接观察模型对象以接收状态更改的通知。然而,这种设计存在一个理论上的问题。*视图对象和模型对象应该是应用程序中最可重用的对象。视图对象代表操作系统和系统支持的应用程序的“观感”;外观和行为的一致性是必不可少的,这需要高度可重用的对象。通过定义建模对象封装与问题域相关的数据,并对该数据执行操作。在设计方面,最好保持模型和视图对象彼此分离,因为这样可以增强它们的可重用性*。(in fact it is quite possible to construct a working application based on the diagram in Figure 7-1. By using the bindings technology, you can easily create a Cocoa MVC application whose views directly observe model objects to receive notifications of state changes. However, there is a theoretical problem with this design. View objects and model objects should be the most reusable objects in an application. View objects represent the “look and feel” of an operating system and the applications that system supports; consistency in appearance and behavior is essential, and that requires highly reusable objects. Model objects by definition encapsulate the data associated with a problem domain and perform operations on that data. Design-wise, it’s best to keep model and view objects separate from each other, because that enhances their reusability.)