C # Delegate and Event watcher Observer design pattern

Source: Internet
Author: User
Tags prototype definition

Objective

Commissioned:

A delegate is a type that holds a reference to a method in an object and is also a type-safe function pointer.

Or a delegate can be seen as a data type that represents a function, similar to a function pointer.

The event is a special delegate
Viewer mode:
Two types of roles:
(1) Subject, subject (monitored object), which often contains content that Observer is interested in.
(2) Observer, observer, it observes Subject. When something happens in Subject (usually when the content of interest changes), it is automatically notified, and Observer takes action (usually to update its own state or display output).

1. Observer Observer mode: Two identities, the monitor and the monitored, are one-to-many relationships.

(1) The person who is being monitored is the publisher of the delegate event, which is the party that defines the delegate event.

(2) The Watcher is the subscriber of the delegate event.

2. Code specification:

(1) The name of the delegate type should end with EventHandler;

(2) The prototype definition of the delegate has a void return value and accepts two input parameters, one is object and the other is the EventArgs type (or inherited from EventArgs).

(3) The name of the event is the remainder of the EventHandler after the delegate is removed.

(4) Types that inherit from EventArgs should end with EventArgs.

3, simple to "heating water heater, the water temperature is greater than 95 degrees when the alarm sounded, display display water temperature" as an example.

Code:

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingSystem.Text;5 6 namespacedotnet-Delegate and Event __observer Observer pattern 27 {8     /// <summary>9     ///This example is: heating water heater, the water temperature is greater than 95 degrees when the alarm sounded, the monitor shows the water temperature. Ten     /// </summary> One     class Program A     { -         Static voidMain (string[] args) -         { theHeater heater =Newheater (); -             //Subscribe to Events -Heater. Boiled + =NewAlarm (). Alarmalert; -Heater. Boiled + =NewDisplay (). showmsg; +             //You can also subscribe to this -Heater. Boiled + =NewHeater.boiledeventhandler (NewAlarm (). Alarmalert); + heater. Boilwater (); A console.readline (); at         } -     } -     /// <summary> -     ///delegate and event implementations: Observer design Patterns (two roles: Monitors and monitors) -     /// </summary> -     classHeater//monitored by (delegate event publisher) in     { -         //Define information that subscribers are interested in, such as: water temperature, type of water heater and Origin to         //Define water temperature +         Private inttemperature; -         //type of water heater the          Public stringType ="Readfire 001"; *         //Origin $          Public stringArea ="China Guangzhou";Panax Notoginseng         /// <summary> -         ///declaring a delegate type the         /// </summary> +         /// <param name= "Sender" >Publisher Object</param> A         /// <param name= "E" >information parameters that are of interest to subscribers</param> the          Public Delegate voidBoiledeventhandler (Object sender,boiledeventargs e); +         /// <summary> -         ///Defining Delegate Events $         /// </summary> $          Public EventBoiledeventhandler boiled; -         /// <summary> -         ///information that subscribers are interested in the         /// </summary> -          Public classBoiledeventargs:eventargsWuyi         { the              Public ReadOnly inttemperature; -              PublicBoiledeventargs (inttemperature) Wu             { -                  This. Temperature =temperature; About             } $         } -         /// <summary> -         ///events that are triggered -         /// </summary> A         /// <param name= "E" ></param> +          Public voidonboiled (Boiledeventargs e) the         { -             if(Boiled! =NULL) $             { theBoiled ( This, e); the             } the         } the         /// <summary> -         ///Trigger Action in         /// </summary> the          Public voidBoilwater () the         { About              for(inti =0; I <= -; i++) the             { the                  This. Temperature =i; the                 if( This. Temperature > the) +                 { -Boiledeventargs e =NewBoiledeventargs ( This. temperature); the onboiled (e);Bayi                 } the             } the         } -     } -     /// <summary> the     ///subscribed by the     /// </summary> the     classAlarm the     { -         /// <summary> the         ///Alarm the         /// </summary> the         /// <param name= "Sender" >The publisher is the watcher object</param>94         /// <param name= "E" >Information of Interest</param> the          Public voidAlarmalert (Object sender, Heater.boiledeventargs e) the         { theHeater heater =(heater) sender;98Console.WriteLine ("Alarm:{0}-{1}", Heater.area, heater.type); AboutConsole.WriteLine ("Alarm: Beep and beep ... Water temperature:"+e.temperature); -         }  101     }102     /// <summary>103     ///subscribed by104     /// </summary> the     classDisplay106     {107         /// <summary>108         ///Display Temperature109         /// </summary> the         /// <param name= "Sender" >The publisher is the watcher object</param>111         /// <param name= "E" >Information of Interest</param> the          Public voidshowmsg (Object sender, Heater.boiledeventargs e)113         { theHeater heater =(heater) sender; theConsole.WriteLine ("Display:{0}-{1}", Heater.area, heater.type); theConsole.WriteLine ("The water is going to boil, the temperature:"+e.temperature);117         }  118     }119}

Operation Result:

C # Delegate and Event watcher Observer design pattern

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.