开发者

Delegation over category

开发者 https://www.devze.com 2023-03-06 02:32 出处:网络
Can any one differentiate when do we 开发者_如何转开发use Delegation over category and vice versa. I am clear over this.

Can any one differentiate when do we 开发者_如何转开发use Delegation over category and vice versa. I am clear over this.

Thanks


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.


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.


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.

@interface NSString (Utilities) 
- (BOOL) isURL; 
@end 

Delegates are similar to callback functions,

0

精彩评论

暂无评论...
验证码 换一张
取 消