5 ms·
I just can't break through delegation. Any suggestions? I do have many years of programming experience (C, Perl, some JS), but can't quite grasp delegation.
by ajays 15y ago
I just can't break through delegation. Any suggestions? I do have many years of programming experience (C, Perl, some JS), but can't quite grasp delegation.
- Xuzz 15y agoApple has their description of delegation here: http://developer.apple.com/library/IOs/#documentation/iPhone/Conceptual/iPhone101/Articles/02_DesignPatterns.html http://developer.apple.com/library/IOs/#documentation/iPhone... Delegation is a design pattern in which one object sends messages to another object that has been specified as its delegate, to ask for input or to notify the delegate that an event is occurring. The delegate object responds to these messages in a custom way. Delegation is a common design pattern in Cocoa Touch apps because it allows you to provide custom behavior without having to subclass and override the methods of a complex framework object, such as UIApplication. It's probably confusing, really, to read about the delegation "design pattern" as on Wikipedia. Instead, it's mostly just a way to avoid subclassing: specify another object, the "delegate", to provide the information or react to an event, rather than do something like subclass and override specific methods for an event. I hope that helped, maybe a tiny bit. If not, here's a +15 voted answer on Stack Overflow about it: http://stackoverflow.com/a/1792862/188512 http://stackoverflow.com/a/1792862/188512
- danilocampos 15y agoIt's (mostly) just events. Look at how many delegate methods contain the words "did" and "will". Object A is going through its life cycle, doing what it does. Certain events there trigger delegate messages. Object B, A's delegate, receives these and does things, or not, as desired, based on the event.
- Zev 15y agoThe way I explained delegates to a friend is: You have this friend, lets say his name is Steve. Steve is a really good guy, and usually excited to hear about what you're working on - and to help out, if he can. He also happens to be really smart and knows everything about everything. He read the dictionary when he was young and continues to read wikipedia daily. So you talk to him when you need to do something[1]. Or maybe you did something and are really excited about it[2]. Perhaps you're about to do something and just need that one last piece of the puzzle before you can finish it up[3]. And if you're unsure about if an idea is worth perusing, Steve is the right person to talk to[4]. Another reason why you're such good friends with Steve is that he is an animal lover and has promised that if you ever call on him to watch your dog because you just need to take a vacation for a weekend, he will watch your dog[5] for you. Since Steve is an animal lover, he might also be able to watch your pet ferret[6]. However, Steve is allergic to cats, and much to his dismay, he cannot watch your cat for you. But, you understand. You can post a notification on Twitter to ask all your other friends if any of them can watch your cat for you[7, 8]. Steve is a delegate. There's only one of him. You only need one. [1-4]. Some examples of when you would use a delegate 5. Protocols can have some methods that their delegate is required to implement, and the compiler will error if they are not implemented. 6. Some methods are optional to implement. 7. Since a method can be optional, it might not be implemented. 8. Notifications are another way for objects in Objective-C to talk to one another. Instead of a 1:1 mapping, they are a 1:Many mapping.
- weaksauce 15y agoThe basics of delegation is that you have classes that could be written so that you must subclass them to do any interesting work. The delegate pattern is such that you have some functions that affect the operation of the sub class(like filling a table full of data) so that it can be customized without subclassing. To delegate is to give responsibility to a subordinate. That's what you are doing; giving some control to a owning class. (not superclass)
- roel_v 15y agoIt's an event system, what you would use function pointers for in C. I think its clearer to be specific and call delegates 'an implementation of the observer design pattern', rather than a design pattern in itself. Other implementations include the class GoF way with subclassing, or typesafe callback abstractions like libsigc or boost::signals in C++. In dynamic languages you'd just store an array with strings of function names to call, or object/method name pairs of methods to call.
- cnunciato 15y agoWell said. Observer is how I got my head around it also.