Delegates - What are they REALLY?

Can someone explain them by analogy with some real processes - for example, with the help of a baseball team or a cafe or an auto-mechanical store - anything that could DO? For example, even if CODE, or SYNTAX, or programming does not say — I have seen many such messages, and none of them really do this for me — can we talk about concepts first?

As I don’t even understand, WHY we have them, how they are profitable, etc.

Show me a real analogy to the process performed with and without delegates, so I can see what makes them so useful and wonderful.

Then we can execute the code.

(And FYI, my particular interest is in the implementation of Objective-C / iPhone App Development, but I really think that understanding this conceptually is, in the first place, much more important.)

early!

+7
objective-c iphone delegates
source share
6 answers

What are they REALLY?

Specialists


Client: Hello - have you fixed my car?

Employee: I believe that we will have a chance to look at it in an hour. I will call you when it is ready.

Client: I already waited 30 minutes, and I'm really in a hurry!

Employee: We are very busy this time of year.

Client: you do not look busy; You were at the reception all the time!

Employee: I am not a mechanic. I am a manager. Do you still want me to lock your brakes?


The manager is good at what he does; he details the problems for customers, orders a flickering liquid, makes a schedule, and makes payroll. Although he loves cars and knows a lot about them, he is not a specialist in their repair. A manager capable of doing a lot of things in the store, but these are mechanics who have the experience necessary to fix client brakes.

One of the most common reasons why delegation is used is to avoid a subclass (it would be like sending a manager to school to become a mechanic - an analogy that closely resembles multiple inheritance). When a subclass can be used for specialization, delegation can often be used instead. Delegation is a loose connection, where (in a good design) one object fulfills all or most of the duties, but often leaves specialized parts of its duties to other implementations (delegates).

+5
source share

Not all coding abstractions have a real equivalent. For example, it will be difficult for you to find a real analogy for paged memory. So instead of making a real analogy, I’ll just give you a real example of why you need them.

First of all, the actual definition of the verb for delegation (from the Oxford Dictionary):

trust (task or responsibility) another person, usually one who is less senior than himself: he delegates routine tasks | The power delegated to him should never be abused.

Delegates are objects to which responsibility has been delegated, because it makes no sense in terms of design for one class to do all the work.

Say you have a UI table class. You want to make it reusable to display many types of data: this means, for the most part, that you cannot bind it to your model. For example, if you specially set up a user interface table to display album objects (with a name, artist, etc.), then you cannot reuse it to display, say, File objects (with owner, document type, etc. ): you will need to edit the class to match. This will duplicate a lot of code, so you should find another mechanism. This leads to a problem: how can it display any data if it is not allowed to bind data classes?

The solution is to share responsibility for receiving data and displaying data. Since your table needs data to display it, but it cannot detect it itself, because it does not know anything about your model, it must delegate this responsibility to another class created specifically for this purpose, and have its information in general form, which the table will know how to use.

This means that you will have another class that will respond to a certain number of methods that will use the table object as if it were part of its main behavior (for example, a method for the delegate to get the name of the columns, another to get the specific strings, etc.). As a specific example, you can find the NSTableViewDelegate link: these are NSTableView methods can call a delegate to get information about various things or individual behavior. (The tableView: argument tableView: in principle, each method identifies the table view that invokes the method. It is often ignored because there are many classes of table view delegates to support a single table view.)

Delegates are objects that simply “know better.” Your user interface table does not have a clue how to get what it needs, but it does have a delegate who “knows better”, so it can request data from it.

You can also use this template to handle events: for example, when you click on a view, he does not know what to do, but perhaps he has a delegate who knows what to do.

In other words, delegates are one of several ways to extend the behavior of a class without modifying or subclassing it.

+8
source share

Delegation is a design pattern. In Cocoa and Cocoa Touch, some framework classes implement this pattern so that you do not need to subclass them to change their behavior.

One typical example of delegation is the decision to close the window or not. When the user clicks the close button, the window delegate will receive -windowShouldClose: and he can either return YES if the window does not have any changes that can be saved, or NO if you want to allow the user to save the document before closing the window.

In another example, an NSStream object receives data, but it does not implement any behavior to do anything with this data. It uses a delegation pattern; when the data is readable, he sends a message to his delegate to inform him that the data is available. Then the delegate can go over and work with the incoming data and tell NSStream continue listening to the additional data or close the connection.

+2
source share

The delegate is kind of like an assistant. Your assistant will answer your phone / email, sending messages back and forth between you and other people. It can also collect the things you need, such as materials or information. In programming, delegates become even more useful. People can learn new abilities on their own, but programs cannot. Instead, you create an object that performs a general function, and use delegates to do specialized work. Thus, you can use as much code as possible, while maximizing the use of as many use cases as possible. The delegate will receive messages from the facility informing him of the status of the operation, as well as providing and receiving data as necessary.

+2
source share

In short, a delegate is a specialist for a specific task.

As a real analogy in the world, think about who owns and operates a bakery with several employees. The man who runs the bakery has an understanding of the high level of baking and makes the main cakes after a special recipe, but her main task is to manage orders for cakes and pastries that come in and ensure that they are well processed.

When an order for a wedding cake arrives, the owner prepares the dough and the main cake, respectively, and delegates the actual baking to a confectionery that specializes in making wedding cakes for processing all the details and icing.

In another scenario, a bakery runner may receive a cake order that has some special requirements. He knows that she can deal with all the basic things, but must check with her staff to tell her whether this request can be fulfilled: firstly, he asks her patissier if he can handle it and maybe what she needs. He then asks his accountant if they have all these ingredients in stock or can get them on time. Thus, the decision on whether the request can be fulfilled is passed on to the employees.

Now replace the bakery runner with UITableView , patissier with id <UITableViewDelegate> and accountant with id <UITableViewDataSource> , and we will return to the talking code.

+2
source share

You can introduce the delegate as secretary to the board of directors. Let's see how to answer the phone and save its agenda / meeting.

Anyone (superclass ?;)) can answer the schedule of phone announcements for the meeting. But every person like you or me does it his own way. So this means that I have the mood anwserPhoneWithMood: (HumanMood *); function other than yours.

Now all the council members will have this too, but they cannot worry about it when they are at work, because they have other things to do, we don’t want to burden them with these black tasks. In addition, it is often unclear who the call is for or if several people should be present at the same meeting. Here we enter the secretariat, she knows how to handle all the different calls, make appointments and have a general overview for the entire board.

Not only is it useful that she answers the call for us (the delegate), but we want her to answer all the calls so that she keeps an overview, these calls are the information she needs to work properly. In addition, any calls for a council member are handled in a fairly general way, so there is no need for each council member to do this in their own way.

In this case, obviously, the secretary is a delegate.

Hope this makes it easier for you.

+1
source share

All Articles