类别授权
任何人都可以区分我们什么时候在类别上使用委派,反之亦然。这一点我很清楚。
谢谢
Can any one differentiate when do we use Delegation over category and vice versa. I am clear over this.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
类别允许添加新方法或覆盖类上的现有方法,从而允许扩展类而无需子类化。添加方法是最有用的目标,如果您不确切知道要扩展的类的用途,则覆盖可能会出错。
它更多的是一种语言功能而不是一种模式,它适用于每个类别。
委托是一种模式而不是语言功能,应该使用的类必须为其编码,否则它将无法工作。
通常,委托需要实现将接收委托的类已知的协议。然后,类将使用委托来做它编码的事情,最常见的一些是发送通知,使用策略模式的一部分,即在代码的某些部分提出问题,以根据具体委托的实现做出决策,让委托执行一个操作或它们的任意组合。
例如
UIApplicationDelegate
是通知 (application:didFinishLaunchingWithOptions:
) 和操作 (application:openURL:sourceApplication:annotation:
) 协议,UITextFieldDelegate 是通知(textFieldDidBeginEditing:
) 和策略 (textField:shouldChangeCharactersInRange:replacementString:
)。实际上我可以想象在我上面提到的所有情况下使用类别来实现委托:发送通知、执行操作、参与策略。但这需要您非常非常清楚扩展类在做什么,可能需要编写代码,否则您可以很容易地破坏该类或被更改的类实现所破坏。所以我认为这种用法是非常错误的。
Category allows to add new methods or overwrite existing methods on a class, thus allows to extend a class without subclassing. Adding methods is the most useful aim, overwriting can go really wrong if you do not know exactly what the class being extended does.
It is more a language feature not a pattern, it works on each class.
Delegate is a pattern not a language feature, the class that is supposed to used must be coded for it, otherwise it won't work.
Usually a delegate will be required to implement a protocol known by the class that is going to receive the delegate. The class will then use the delegate to do stuff it was coded for, some of the most common are sending notifications, using a part of a strategy pattern, that is asking question in certain part of code to make decisions based on the concrete delegate's implementation, letting delegate execute an action or any combination of them.
For example
UIApplicationDelegate
is a notification (application:didFinishLaunchingWithOptions:
) and action (application:openURL:sourceApplication:annotation:
) protocol, UITextFieldDelegate is notification (textFieldDidBeginEditing:
) and strategy (textField:shouldChangeCharactersInRange:replacementString:
).Actually I can imagine using category to implement delegating in all this cases I stated above: sending notifications, doing actions, taking part in a strategy. But it would require you to very very good know what to extended class is doing, probably to have it code, otherwise you can very easily break the class or be broken by changed class implementation. So this usage would be in my opinion highly wrong.
类别:向现有类添加方法。
委托:通过允许某个其他对象(委托)参与对象的操作来修改另一个对象的行为。
假设您有一个包含三个表的 iOS 应用程序。尽管它们的配置方式相同,但如果这些表具有不同的委托,它们的行为可能会有所不同。
您不能使用类别来做到这一点,因为类别同样适用于它扩展的类的所有实例。另一方面,如果您想扩展 UITableView 类以添加一些新功能,则需要一个类别*。委托是特定于实例的,并且仅限于委托类的设计者为其设想的角色。
*当然是一个子类。
category: Adds methods to an existing class.
delegate: Modifies behavior of another object by allowing some other object, the delegate, to participate in the object's operation.
Say you've got an iOS application with three tables. Even though they're configured the same way, those tables each may behave differently if they have different delegates.
You can't do that with a category because a category applies equally to all instances of the class it extends. On the other hand, if you want to extend the UITableView class to add some new capability, you need a category*. Delegates are instance-specific and limited to the role envisioned for them by the designer of the delegating class.
*or a subclass, of course.
当你需要扩展类而不创建子类时,例如当你需要向 NSString 添加一个名为 isURL 的方法时,你可以使用类别,如下所示,这里我们不创建子类,而是扩展执行。
委托类似于回调函数,
Categories are used when you need to extend the class without creating a subclass, for example when you need to add a method named isURL to the NSString you can make use of categories as follows, here we not creating a subclass, instead we are extending the implementation.
Delegates are similar to callback functions,