. NET brief introduction to component programming (relationship between delegate and event)

Source: Internet
Author: User

I have been learning about the. NET Component Programming Technology for some time recently. There are many good things to share with you during the learning process. [Wang qingpei has all rights reserved. For more information, please sign it.]

What is component programming? In fact, it is to use. NET to develop programs based on component models. It is a high level and has not been studied and honed for a long time.

I cannot feel this feeling. Our current development philosophy should be object-oriented, but how to improve this level is only a matter of learning.

In fact, component-Oriented Programming involves the idea of object-oriented programming, which encapsulates a set of functions for reuse in the future. However, in the process of component development, the idea of object-oriented should be used to construct

The two concepts of component creation are not contradictory. The object-oriented thinking is concrete implementation, while the component-oriented thinking should be described at a macro level at the height of a product or framework.

We will not discuss these technical philosophies. learning practical things is more realistic. We only need to know That object-oriented programming is code reuse, while component-oriented programming is implementation reuse. Regardless of the surface

Objects or components-oriented components must be implemented with good code in the final analysis. As long as we start coding, we can inject these ideas into our minds.

In this article, we will first remove some technical questions about delegation and events that are frequently used in component programming. Before learning component programming, I cannot understand the relationship between delegate and event and

What is the intention between the users. However, I had a rough understanding in the recent learning process and decided to write the learned technology and share it with you. [Wang qingpei has all rights reserved. For more information, please sign it.]


In C #, delegate represents the concept of delegation, while event represents the concept of event. But why do they need to be used together? You can implement object-oriented events without event.

All subscribers can be called through multicast delegation.

In fact, I want to explain why we need to use the event mechanism in one sentence, but I will not talk about it here, so it will be meaningless. Please continue.


Let's first look at the Code:

1:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Text;
  4.  
  5. Namespace EventDemo
  6. {
  7. /// <Summary>
  8. /// Handle remove delegate
  9. /// </Summary>
  10. /// <Param name = "handle"> message </param>
  11. Public delegate void MoveHandler (string handle );
  12. /// <Summary>
  13. /// Handle object
  14. /// </Summary>
  15. Public class ObjectHandler
  16. {
  17. /// <Summary>
  18. /// Handle event
  19. /// </Summary>
  20. Public MoveHandler hand;
  21. /// <Summary>
  22. /// Trigger the handle event to notify all subscribers
  23. /// </Summary>
  24. Public void Onhand ()
  25. {
  26. Hand ("handle event ");
  27. }
  28. }
  29. }

2:

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4.  
  5. namespace EventDemo  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             ObjectHandler handermanager = new ObjectHandler();  
  12.             handermanager.hand += handmethod;  
  13.             handermanager.Onhand();  
  14.         }  
  15.         public static void handmethod(string eventstring)  
  16.         {  
  17.             Console.WriteLine(eventstring);  
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21. }  


To demonstrate the effect, I randomly named an object, which has no special significance.


I defined an ObjectHandler object and used MoveHandler internally to delegate the instance to represent the event object. When the Onhand method is called, I delegate the instance to call subscribers one by one to achieve the event effect. However, the method for triggering an event is generally not defined as public, but only for display.

[Wang qingpei has all rights reserved. For more information, please sign it.]
We introduce the problem that an event is private within an object. It can only be triggered by an object to comply with the object-oriented rules, but it cannot be achieved by using delegation alone. The delegated instance must be subscribed by the subscriber, so the delegated instance must be public. At this time, the delegated instance cannot guarantee who triggered the event.
See the Code:

3:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Text;
  4.  
  5. Namespace EventDemo
  6. {
  7. Class Program
  8. {
  9. Static void Main (string [] args)
  10. {
  11. ObjectHandler handermanager = new ObjectHandler ();
  12. Handermanager. hand + = handmethod;
  13. Handermanager. Onhand ();
  14.  
  15. /* I cheat all subscribers by triggering internal delegation of the object.
  16. All subscribers think that the events sent by ObjectHandler are actually simulated by someone secretly. */
  17. Handermanager. hand ("killing with a knife ");
  18. }
  19. Public static void handmethod (string eventstring)
  20. {
  21. Console. WriteLine (eventstring );
  22. Console. ReadLine ();
  23. }
  24. }
  25. }

The subscriber has no reason to take responsibility for this unexpected event. The ObjectHandler object must take responsibility for this.

So how to avoid events from being triggered by malicious users? This requires the event keyword. If the delegated instance after the event is modified, it cannot be triggered outside the object. See the Code:

4:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Text;
  4.  
  5. Namespace EventDemo
  6. {
  7. /// <Summary>
  8. /// Handle remove delegate
  9. /// </Summary>
  10. /// <Param name = "handle"> message </param>
  11. Public delegate void MoveHandler (string handle );
  12. /// <Summary>
  13. /// Handle object
  14. /// </Summary>
  15. Public class ObjectHandler
  16. {
  17. /// <Summary>
  18. /// Handle event
  19. /// </Summary>
  20. Public event MoveHandler hand;
  21. /// <Summary>
  22. /// Trigger the handle event to notify all subscribers
  23. /// </Summary>
  24. Public void Onhand ()
  25. {
  26. Hand ("handle event ");
  27. }
  28. }
  29. }


In this way, the object closure principle is well maintained.

In the next article, we will learn the deeper application of delegation "Asynchronous delegation"

This article is from the pattern driven the world blog, please be sure to keep this source http://wangqingpei557.blog.51cto.com/1009349/639337

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.