Understanding delegation and events from another perspective

Source: Internet
Author: User

If you are still confused about delegation and events, you can try to answer the following questions:
Let you design a framework (or program) to achieve the effect: click the button to implement label1.text = "Haha, click it for the first time !", What do you do? Note: Do not use a framework that has been implemented by. net.
We will think that there should be a listener in the windows system to handle the mouse click event. Once the button is clicked, it will execute a certain program. It should be like the following pseudo code:
ListenerMethod (){
If (button. clicked)
{......}
}
I can write label1.text = "Haha, click it for the first time" in!
If you want to implement other content, such as clicking again, label. text = "No problem. Click again ".
So far, have you found any problems?
If no problem is found, you will have a problem.
I also thought of it after learning the "Design Pattern". If we follow the above method, we will:
1. You must publish the specific content of the ListenerMethod () method. Otherwise, how can someone use it? Where can I write the executed code?
2. It is not good to publish methods. Why? The simplest thing is, how did hackers come out of it if they are afraid of seeing something bad? Well, this is actually just one aspect. The more practical reason is to prevent misoperation ...... Not much. Understand the keyword "encapsulation "!
Then I thought about how to expose the ListenerMethod () method.
Can I write a method in advance and put it in {}? Others write the implementation code elsewhere, for example:
ListenerMethod (){
If (button. clicked ){
TheMethod (); // execute this method as long as the button is clicked;
}
}
Set the method name and parameters in other places to fill in the blanks, as shown below:
TheMethod (){
...... // The content is filled in by others (the professional word is "user ").
}
Haha, it's a bit like. We seem to write the implementation program in the buttonClick () method.
In this case, there is still a problem. Can you think of it?
Now we are a button. What if there are two buttons to implement different functions? I wrote two if statements! Three, four ...... Actually, I still don't know how many frameworks will be there when I build the framework ~~
Think again!
This is about to go back to the top conjecture. Is there a correspondence between the button and Listener?
How can this correspondence be achieved?
We can imagine that there is a monitoring center in the computer system that manages the mouse and clicks the button. Each button can go to this center to "register" its corresponding method. In this way, when a specific button is clicked, the listener center can execute the corresponding method based on the previous "Registration.
It seems that the above method is good, like yourself. So how can we implement the above ideas? Of course, it is to use delegation and events.

Maybe you still haven't figured it out. So do I. Let's try to write a program. To break away from existing frameworks such as winform, we will build a console program.

Code before delegation or event is not used.

Code
Class Program
{
Static void Main (string [] args)
{
// Instantiate a button class
Button btn = new Button ();
Btn. Click ();
}
}

// First define a Button class, which should have a Click Method
Public class Button
{
// Call a fixed method using the Click Method
Public void Click ()
{
ForCustome fc = new ForCustome ();
Fc. Button_Click ();
}
}
// The above class is encapsulated and invisible to users, or it can be understood that users cannot change it.

// The following class is provided to the user
Public class ForCustome
{
Public void Button_Click ()
{
// The user writes the specific implementation method here
Console. WriteLine ("clicked! ");
}

}

 

It can be seen that the above Code is feasible to implement a button, but the two buttons are troublesome. If we don't feel unwilling to go to the Yellow River, let's just write two more buttons.

 

Code
Class Program
{
Static void Main (string [] args)
{
// Instantiate a button class
Button btn = new Button ();
Btn. Click ();

// Instantiate the second button
Button btn2 = new Button ();
Btn. Click (); // can this write?
}
}

// First define a Button class, which should have a Click Method
Public class Button
{
// Call a fixed method using the Click Method
Public void Click ()
{
ForCustome fc = new ForCustome ();
Fc. Button_Click ();

// I 'd Like to modify it here and add a method call.
Fc. Button2_Click (); // it would be better if there is another conditional judgment logic, right?
}
}
// The above class is encapsulated and invisible to users, or it can be understood that users cannot change it.

// The following class is provided to the user
Public class ForCustome
{
Public void Button_Click ()
{
// The user writes the specific implementation method here
Console. WriteLine ("clicked! ");
}

// Adding a method is required for implementation
Public void Button2_Click ()
{
Console. WriteLine ("I am the second button to be clicked! ");
}
}

 

If you are writing it yourself, it is estimated that you have crashed (I am doing this anyway. If you don't feel this code, try to write it.

Okay. Let's see how Microsoft solves this problem.

 

Code
// Declare a delegate first. The goal is to call the methods in the ForCustome class. Therefore, pay attention to the method signature.
Public delegate void myDelegate ();

 
Public class Button
{
// Declare an event and associate it with the delegate
Public event myDelegate ClickIt;

Public void Click ()
{
// As you can imagine, this method is a "complex" that encapsulates a large segment of condition judgment statements we wanted previously"
ClickIt ();
}
}

Class Program
{
Static void Main (string [] args)
{
ForCustome fc = new ForCustome ();

// Instantiate a button class
Button btn = new Button ();
// Here, we associate the button event with the (to be triggered) method corresponding to the button event, which is equivalent to the registration we previously imagined.
Btn. ClickIt + = new myDelegate (fc. Button_Click );
Btn. Click ();

// Instantiate the second button
Button btn2 = new Button ();
Btn2.ClickIt + = new myDelegate (fc. Button2_Click );
Btn2.Click (); // can this write?
}
}

// This class is provided to the user and remains unchanged.
Public class ForCustome
{
Public void Button_Click ()
{
// The user writes the specific implementation method here
Console. WriteLine ("clicked! ");
}

// Adding a method is required for implementation
Public void Button2_Click ()
{
Console. WriteLine ("I am the second button to be clicked! ");
}
}

 

Success! An elegant architecture. If you want to add another button and corresponding event, will you do it?

In winform and asp.net, two parameters are added to the method corresponding to the event and the naming rules are standardized, which is more perfect.

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.