What is Delegate? [closed]

2019-01-15 23:40发布

I am confused that what is the actual role of a delegate?

I have been asked this question many times in my interviews, but I don't think that interviewers were satisfied with my answer.

Can anyone tell me the best definition, in one sentence, with a practical example?

标签: oop delegates
13条回答
一夜七次
2楼-- · 2019-01-16 00:18

Taken from here

Q What are delegates?
A When an object receives a request, the object can either handle the request itself or pass the request on to a second object to do the work. If the object decides to pass the request on, you say that the object has forwarded responsibility for handling the request to the second object.

Or, as an easy pseudo example: something sends a request to object1. object1 then forwards the request and itself to object2 -- the delegate. object2 processes the request and does some work. (note: link above gives good examples)

查看更多
疯言疯语
3楼-- · 2019-01-16 00:19

A delegate is something to which a task is being delegated. The primary purpose of delegation is to decouple code and allow for greater flexibility and reuse.

In programming, and specifically object-oriented programming, this means that when a method is called to do some work, it passes the work on to the method of another object that it has a reference to. The reference could point to whatever object we wish, as long as the object conforms to a predefined set of methods. We call it "programming to an interface" (versus programming to a concrete class implementation). An interface is basically a generic template and has no implementation; it simply means a recipe, a set of methods, preconditions and postconditions (rules).

Simple example:

SomeInterface
{
   public void doSomething();
}


SomeImplementation implements SomeInterface
{
   public void doSomething()
   {
      System.err.println("Was it good for you?");
   }

}


SomeCaller
{
   public void doIt(SomeInterface someInterface)
   {
      someInterface.doSomething();
   }
}

Now you see I can use whatever implementation I want at any time without changing the code in SomeCaller because the type that doIt() is passed is not concrete but rather abstract since it's an interface. In the Java world, this is often expressed in the service paradigm where you call out to a service (an object advertising itself as a service via a specific interface) and the service then calls out to delegates to help it do its work. The service's methods are named as coarse-grained tasks (makePayment(), createNewUser(), etc.), while internally it does lots if nitty-gritty work through delegation, with the delegates' types being interfaces instead of the concrete implementations.

SomeService
{
    SomeInterface someImplementation = ... // assign here
    SomeOtherInterface someOtherImplementation = ... // okay, let's add a second

    public void doSomeWork()
    {
         someImplementation.doSomething();
         someOtherImplementation.doSomethingElse();
    }
}

(N.B.: How an implementation gets assigned is beyond the scope of this thread. Lookup inversion of control and dependency injection.)

查看更多
再贱就再见
4楼-- · 2019-01-16 00:23

A delegate is an object that represents a pointer to a function. However, it is not an ordinary function pointer in that it:

1) Is Object Oriented

2) Is type safe, i.e. it can only point to a method and you cannot read the raw memory address it is pointing to

3) Is strongly typed. It can only point to methods that match its signatures.

4) Can point to more than one method at the same time.

查看更多
劳资没心,怎么记你
5楼-- · 2019-01-16 00:25

A delegate object is an object that another object consults when something happens in that object. For instance, your repair man is your delegate if something happens to your car. you go to your repair man and ask him to fix the car for you (although some prefer to repair the car themselves, in which case, they are their own delegate for their car).

查看更多
再贱就再见
6楼-- · 2019-01-16 00:28

While not really a "function pointer", a delegate might look like this is a dynamic language like PHP:



$func = 'foo';
$func();

function foo() {
    print 'foo';
}

or in JavaScript you could do something like:


var func = function(){ alert('foo!'); }
func();

查看更多
Viruses.
7楼-- · 2019-01-16 00:33

A great explanation and practical implementation of the Delegate pattern can be found in the Google Collections Forwarding Classes (also, the Decorator pattern).

查看更多
登录 后发表回答