数据源和委托之间有什么区别? [英] What's the difference between data source and delegate?
问题描述
我有一个与Cocoa框架设计模式相关的基本问题。
委托和数据源有什么区别? 两者都可以使用 > UI / NSTableView 是 注意:
@protocols
声明,但是一些类或框架使用 delegate
,其他一些使用 datasource
。
委托
响应UI相关事件,而数据源
纯粹与数据相关。但是,我不知道Cocoa的UI类之外的任何数据源实现。
p>代理和数据源模式在很大程度上是独立的,并且是正交的:
代理模式在Cocoa中非常常见,允许一个委托(任何实现非正式委托协议到OS X 10.6或10.6和更高版本中的正式委托 @protocol
)来修改对象实例的行为。这种模式通常用来代替子类化:而不是子类化一个类来改变它的行为,你提供一个响应适当的方法的代理。使用委托的类在合同事件中向其委托发送消息。类和委托之间的API由类定义,并且对于使用该模式的每个类都是不同的,但是API通常由请求代理如何处理特定事件的消息组成。委托模式比子类化的一个优点是,类可以实现多个委托协议,允许它的实例充当多个类的委托。类似地,对象实例可以是多个其他对象的委托(因此大多数委托API将对象作为API中每个消息的第一个参数传递)。代理模式在其他UI框架中并不常见(虽然Qt在其模型/视图框架中使用了委托模式),并且不是.Net / CLR委托,它们本质上是类型函数
数据源模式通常由Cocoa中的 NSView
子类使用,它们具有复杂的状态数据如NSBrowser,NSTableView,NSOutlineView等。数据源协议定义了这些(和其他)类的实例可以使用以获得在视图中显示数据的API。虽然 NSController
和Cocoa Bindings架构已经取代了许多数据源模式的使用,但它仍然是常见的和非常强大的。和上面描述的委托模式一样,它的部分功能来自一个能够作为多个数据源使用实例(甚至可能是具有不同数据源协议的多个类的实例)的数据源的对象。数据源模式通常用在其他UI框架中,例如Qt(在模型/视图框架中,其中模型类似于数据源)和WPF / Silverlight(其中数据源可能更接近于视图模型)。
I have a fundamental question related to Cocoa frameworks design patterns.
What's the difference between delegate and data source?
Both of them could use @protocols
declaration, but some classes or frameworks are using delegate
, and some others are using datasource
.
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.
Note:
- The delegate I mentioned in this question is not always related to UI events.
- The data source question has been answered.
The delegate and datasource patterns are largely independent, and orthogonal:
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.
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屋!