Why do I need to use delegation?

Source: Internet
Author: User

The following are my comments. If there are any errors, please point out in time that some code is typed in notepad and compilation may not pass. Sorry.

For C # beginners, I am afraid that the most common problems are events. But now we will not talk about events. Let's look at the benefits of delegation.
Assume there is a class

Class Student
{
// For ease of use, no attribute is required.
Public int id;
Public string name;
Public int age;
Public int classId; // assume there is a class number.
 
Public Student (int id, string name, int age, int classId)
{
//...
}
}

Class program
{
Static void main (String [] args)
{
// Declare a Student array. Do not drill down here,
// Why not use List or List <T>, because both are Microsoft-encapsulated data structures that enable sorting and do not need to be illustrated.
Student [] s = {
New Student (1004, "Cheng ),
New Student (1002, "Zhang Cai ),
New Student (1003, "Li Dawei ),
New Student (1005, "Bingbing)
New Student (1001, "Li Xiaohong ),
}
// Sort
Sort (s); // complete sorting




}
 
// Implement the Bubble sorting method
Private void Sort (student [] s)
{
For (int I = 0; I <s. Length-1; I ++) // control the number of turns
{
For (int j = 0; j <s. Length-1-I; j ++) // control the number of exchanges
{
If s [j]. id> s [j + 1]. id)
{
Student temp = S [j];
S [j] = s [j + 1];
S [j + 1] = temp;
}
}
}

}
}

In the above process, we sorted the Student ID by a simple method. This method has no return value and receives a Student array. Now it satisfies the proposition and has no problem at all.

A new problem arises. We hope that not only the student id, but also the student age can be used for sorting. The easiest way to think of this is to use Switch... Case.
Private void Sort (student [] s, sting sortfield)
{

Switch (sortfield)
Case "id ":

For (int I = 0; I <s. Length-1; I ++) // control the number of turns
{
For (int j = 0; j <s. Length-1-I; j ++) // control the number of exchanges
{
If s [j]. id> s [j + 1]. id )//***********************************
{
Student temp = S [j];
S [j] = s [j + 1];
S [j + 1] = temp;
}
}
}
Break;

Case "age ":
For (int I = 0; I <s. Length-1; I ++) // control the number of turns
{
For (int j = 0; j <s. Length-1-I; j ++) // control the number of exchanges
{
If (s [j]. age> s [j + 1]. age )//******************************
{
Student temp = S [j];
S [j] = s [j + 1];
S [j + 1] = temp;
}
}
}
Break;

}

Now it turns
Sort (s, "id") or Sort (s, "age ")
Note that we can write a method for the asterisks.
 
// Sort by Student Object id
Private bool CompareById (Student s1, Student s2)
{
Return s1.id> s2.id;
}
 
Private bool CompareByAge (Student s1, Student s2)
{
Return s1.Age> s2.Age;
}

So the Sort method becomes
Private void Sort (student [] s, sting sortfield)
{

Switch (sortfield)
Case "id ":

For (int I = 0; I <s. Length-1; I ++) // control the number of turns
{
For (int j = 0; j <s. Length-1-I; j ++) // control the number of exchanges
{
If (CompareById (s [j], s [j + 1]) //***********************************
{
Student temp = S [j];
S [j] = s [j + 1];
S [j + 1] = temp;
}
}
}
Break;

Case "age ":
For (int I = 0; I <s. Length-1; I ++) // control the number of turns
{
For (int j = 0; j <s. Length-1-I; j ++) // control the number of exchanges
{
If (CompareByAge (s [j], s [j + 1]) //******************************
{
Student temp = S [j];
S [j] = s [j + 1];
S [j + 1] = temp;
}
}
}
Break;

}

The new problem comes again. Now I want to sort by class numbers (though not necessarily appropriate). The easiest way to think of this is to add a case, if there are other requirements, you will add more cases
This is of course not conducive to program expansion. paste and copy cannot meet the changing needs. We hope our method will become
// Implement the Bubble sorting method
Private void Sort (student [] s)
{
For (int I = 0; I <s. Length-1; I ++) // control the number of turns
{
For (int j = 0; j <s. Length-1-I; j ++) // control the number of exchanges
{
If (s [j]. id> s [j + 1]. id) // *************************** CompareById (s [j], s [j + 1] Or CompareByAge (s [j], s [j + 1])
{
Student temp = S [j];
S [j] = s [j + 1];
S [j + 1] = temp;
}
}
}

}
Note that we can use the asterisk (*) here to sort data by comparison. You do not need to copy the data and add more cases!
Everyone knows that everything with the same features can be abstracted into a class. Can the methods be abstracted? If the method can be abstracted, we only need to write a form parameter in the place where the asterisk is played.
What are the same features of CompareById and CompareByAge? Yes, except for method names, they have the same return value bool and same input parameters,

At this time, the Commission was born for operation. What is the Commission? Delegation is an abstraction of methods. For example, you and I can be abstracted into the Person class,
CompareById and CompareByAge are abstracted
Public delegate bool CompareDelegate (Student s1, Student s2 );

So our Sort method becomes
Private void Sort (Student [] s, CompareDelegate method)
{
For (int I = 0; I <s. Length-1; I ++) // control the number of turns
{
For (int j = 0; j <s. Length-1-I; j ++) // control the number of exchanges
{
If (method (s [j], s [j + 1]) // call
{
Student temp = S [j];
S [j] = s [j + 1];
S [j + 1] = temp;
}
}
}

}

So the current call becomes
Sort (s, CompareById) or Sort (s, CompareByAge );

If you want to sort by class, simply write a method
Private bool CompareByClassid (Student s1, Student s2)
{
Return s1.classid> s2.classid;
}

Then the call is changed to Sort (s, CompareByClassid). You can also see the scalability.

Finally, the delegate is an abstraction of the method.

 

Thank you for visiting my blog. It would be a great honor to keep your footprints.

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.