C # re-warfare delegate event multithreading 2_ delegate and event combination to reduce module low coupling __c#

Source: Internet
Author: User

I've been optimizing a WinForm form application to show how to lower the module's low coupling through "delegates" and "events."

But before that, we have to understand what a low coupling is.

The simple thing is that two modules, if a class in a module, you need to instantiate a class in another module, and then call the method so that the two modules are tied to a chariot, and if a module disappears suddenly, an error is made, and all we have to do is add the method in a module to a delegate or event. , waiting for other modules to register, and whether the other modules disappear, regardless of the module that owns the delegate or event. Listen, it's still a wrap, example.

/*------------------------------------------------------------------------------------------------------Split Line-------------- ---------------------------------------------------------------------------------------------------------*/

One.. Purpose: To have two forms (ParentForm and ChildForm), each owning a text box, sending a message to another form using one form, and displaying it in a text box.

Two. Specific operation:

1. One of the stupidest ways.

Create a new Winfrom application, add controls, and change the naming.

Show Results:

A parent form directly manipulating a subform's data, which, in the object-oriented view, is extremely stupid. We're going to improve on this, and maybe we'll do it in the subform, provide the method, make the modification, and the benefits can be verified to see if the input is correct.

2. Seemingly good way to change

Add a method to the subform to modify the txt_receive of the subform

Set the form text box as private

To call a method in the parent form code:

This approach is at least object-oriented, but unfortunately, it's still a rookie way of writing,

The disadvantage of this approach is that if I delete the ChildForm, the main form crashes, and if I have many forms associated with the parent form, I want to modify the code a lot. Such a two-form body is coupled, and we want to decouple, that is, no instances or code of other forms can appear in the parent form.

3. Decoupling, using the delegate.

If you want to avoid the error in 2, we should not have other forms (modules) of the object in the parent form.

So if we add the form again, the Click event of our parent form doesn't need to change.

Delegates are not very secure, and we use events, events, and delegates as relationships between private variables and attributes.

4. Use Event

That is, when the button is clicked, the delegate is not invoked directly, but the event is triggered.

Add a Class

Results run successfully.

Finally, the difference between the event and the delegate is emphasized:

1. Event is a special instance of a delegate

2. Events can only be triggered within the current class, but can be other places + =, so secure.

3. No, other methods of use, events and delegates are the same.

Related Article

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.