数据源和委托有什么区别? [英] What's the difference between data source and delegate?

查看:28
本文介绍了数据源和委托有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个与 Cocoa 框架设计模式相关的基本问题.

I have a fundamental question related to Cocoa frameworks design patterns.

委托和数据源有什么区别?

它们都可以使用@protocols 声明,但有些类或框架使用delegate,而另一些使用datasource.

Both of them could use @protocols declaration, but some classes or frameworks are using delegate, and some others are using datasource.

我从 UI/NSTableView 中可以理解的是 delegate 响应 UI 相关的事件,而 datasource 纯粹与数据.但是,我不知道 Cocoa 的 UI 类之外的任何数据源实现.

All I can understand from UI/NSTableView is the delegate respond to UI-related events, while the datasource is purely related to the data. But, I don't know any data source implementations outside the UI classes of Cocoa.

注意:

  • 我在这个问题中提到的委托并不总是与 UI 事件相关.
  • 已回答数据源问题.

推荐答案

委托和数据源模式在很大程度上是独立的,并且是正交的:

The delegate and datasource patterns are largely independent, and orthogonal:

委托模式在 Cocoa 中非常常见,它允许委托(在 OS X 10.6 之前实现非正式委托协议的任何实例,或在 10.6 和更高版本中实现非正式委托协议的正式委托 @protocol)修改对象实例的行为.这种模式经常被用来代替子类化:不是子类化一个类来改变它的行为,而是提供一个响应适当方法的委托.使用委托的类在约定的事件中向其委托发送消息.类和委托之间的 API 由类定义,并且对于使用该模式的每个类都不同,但 API 通常由询问委托如何处理特定事件的消息组成.委托模式相对于子类化的一个优点是一个类可以实现多个委托协议,允许其实例充当多个类的委托.类似地,一个对象实例可以是多个其他对象的委托(因此大多数委托 API 将对象作为第一个参数传递给 API 中的每个消息).委托模式在其他 UI 框架中并不常见(尽管 Qt 确实在其模型/视图框架中使用了委托模式),并且与本质上是类型化函数的 .Net/CLR 委托不同指针.

The delegate pattern is very common in Cocoa and allows a delegate (any instance implementing the informal delegate protocol prior to OS X 10.6, or the formal delegate @protocol in 10.6 and later) to modify the behavior of an object instance. This pattern is often used instead of subclassing: instead of subclassing a class to change its behavior, you supply a delegate that responds to the appropriate methods. Classes that use delegates send messages to their delegate at contracted events. The API between class and delegate is defined by the class and is different for each class that uses the pattern, but the API generally consists of messages asking the delegate how to handle a particular event. One advantage of the delegate pattern over subclassing is that a class may implement multiple delegate protocols, allowing its instances to act as delegate for multiple classes. Similarly, an object instance can be the delegate for multiple other objects (hence most delegate APIs pass the object as the first argument to each message in the API). The delegate pattern is not as common in other UI frameworks (though Qt does use the delegate pattern in its Model/View framework), and is not the same as .Net/CLR delegates which are essentially typed function pointers.

数据源模式经常被 Cocoa 中具有复杂状态数据的 NSView 子类使用,例如 NSBrowser、NSTableView、NSOutlineView 等.数据源协议定义了一个 API,这些 API 的实例(和其他)类可用于获取要在视图中显示的数据.尽管 NSController 和 Cocoa Bindings 架构已经取代了数据源模式的许多用途,但它仍然很常见并且非常强大.就像上面描述的委托模式一样,它的部分功能来自一个对象,它能够充当多个使用数据源的实例(甚至可能是具有不同数据源协议的多个类的实例)的数据源.数据源模式通常用于其他 UI 框架,例如 Qt(在 Model/View 框架中,模型类似于数据源)和 WPF/Silverlight(其中数据源可能更类似于视图模型)).

The data source pattern is often used by NSView sub-classes in Cocoa that have complex state data such as NSBrowser, NSTableView, NSOutlineView, etc. The data source protocol defines an API that instances of these (and other) classes may use to get the data to display in the view. Although the NSController and Cocoa Bindings architectures have replaced many uses of the data source pattern, it's still common and very powerful. Like the delegate pattern described above, part of its power comes from an object being able to act as the data source for multiple data-source-using instances (and possibly even instances of multiple classes that have different data source protocols). The data source pattern is used commonly in other UI frameworks, such as Qt (in the Model/View framework where the model is analogous to the data source) and WPF/Silverlight (where the data source might be more closely analogous to the view model).

这篇关于数据源和委托有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆