Java Interview Essentials delegates in 006---. NET and proxy patterns and delegates in Java

Source: Internet
Author: User

1.1.1 Definition

A delegate is a type of reference method. Once a method is assigned to a delegate, the delegate has exactly the same behavior as the method. The use of a delegate method can have parameters and return values, like any other method, as shown in the following example:

Code in C #

public delegate int performcalculation (int x, int y);

Any method that matches the signature of the delegate (consisting of the return type and parameters) can be assigned to the delegate.

A simple understanding of a delegate delegate (or proxy) is a data type: its variables can be referenced to a conforming method, which can be called indirectly by a delegate.

In fact. NET delegate is similar to the C language function pointer, the difference is. NET delegate is type-safe, which means that the function pointer in C is nothing more than a pointer to a storage cell, and we cannot say what the pointer actually points to. The 1.1.2 delegate uses four parts of a delegate: To define a delegate-type delegate execution, call the method to define a delegate instance delegate instance

We first define a delegate type as follows:

    
    
     
     
     
     Customizing a delegate type
     
     

Public delegate void stringprocessor ( string input);

We then define the candidate delegate method in 5 as follows:

void printstring ( string

void Printinteger ( int

void printtwostrings ( string x, string

int getstringlength ( string

void PrintObject ( object x)

Guess which one matches the delegate type signature provided above (signature matching: parameter type, number of arguments and return type matching). The moment of excitement is up. Publish the answer, and the method of the delegate type matching is printstring and printobject, if there is not clear please consider the criteria for the delegate matching-signature matching.

Figure 1 Delegate successful output

Now that we have a certain understanding of the delegate, next we will introduce the place where delegates are most often used-events.

We will discuss events from the perspective of the transmitter and the receiver, for example, in UI programming, a mouse click or a keyboard key, and the transmitter is. NET's CLR, note that the event dispatcher does not know who the receiver is, this conforms to the object-oriented principle, and an event receiver has a method to handle the event, at which point it is entrusted, as the event dispatcher knows nothing about the event receiver, and by delegating as an intermediary, The receiver registers the event-handling method with the event, which implements the process by which the transmitter-> the delegate-> the receiver.

We can say this: a delegate is a class that defines the type of a method so that it can be passed as a parameter to another method, which dynamically assigns the method to the parameter, avoiding the use of if-else (Switch) statements in the program, and making the program more extensible.

1.1.3 Custom Delegate

It's a bit hard to understand, and then we'll take a concrete example to analyze what a delegate is and how to implement a delegate. Now not very much like to engage in multi-language. Let's see how we can make our programs speak multiple languages.

<summary>
The 中文版 speaker.
</summary>
<param name= "name" > "Name". </param>
public void Englishspeaker (string name)
{
Console.WriteLine (
String. Format ("Hello My name is {0} and I am 中文版 speaker.\n", name));
}

<summary>
The chineses speaker.
</summary>
public void Chinesespeaker (string name)
{
Console.WriteLine (
String. Format ("Hello my name is {0}, I speak Mandarin.") \ n ", name));
}

Okay, now we have two ways to speak Mandarin and English respectively, and now our program speaks Mandarin and English. Now we are considering when to speak Putonghua when to speak English, it is not easy to add a judgment on the OK, yes we can through the switch or if else can be achieved.

<summary>
Call different methods based on context
</summary>
<param name= "Name" > string </param>
<param name= "lang" > Enum </param>
private static void Say (string name, L Anguage Lang)
{
switch (lang)
{
Case Language.chinese:
Program.chinesespeaker (name);
break;
Case Language.english:
Program.englishspeaker (name);
break;
Default:
break;
}
}

But suppose we now have to add a new language to Spanish, and we can add Spanish, but we have to modify the switch statement to increase the judgment, which does not conform to the OCP in OOP (for extended opening, for the Modify closure principle), at which point the delegate should be present.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.