#region //define the Boiledeventargs class and pass the information that is of interest to observer Public classBoiledeventargs:eventargs { Public ReadOnly inttemperature; PublicBoiledeventargs (inttemperature) { This. Temperature =temperature; } } //Water Heaters Public classheared { Public stringType ="Realfire 001";//Add model as demo Public stringArea ="China Xian";//Add Origin as demo Private inttemperature; Public Delegate voidBoidhandler (Object sender, Boiledeventargs e);//declaring a delegate Public EventBoidhandler boidevent;//declaring Events//can be overridden by a class that inherits from Heared, so that the inheriting class rejects its monitoring by other objects protected Virtual voidonboiled (Boiledeventargs e) {if(Boidevent! =NULL) { //If an object is registeredBoidevent ( This, e);//methods to invoke all registered objects } } //Boiling Water Public voidBoilwater () { for(inti =0; I < -; i++) {Temperature=i; if(temperature> the) { //establishes the Boiledeventargs object. Boiledeventargs e =NewBoiledeventargs (temperature); Onboiled (e); //calling the Onbolied method } } } } //Alarm Public classAlarm { Public voidMakealert (Object sender, Boiledeventargs e) {heared heater= (heared) sender;//is this very familiar? //accessing public fields in senderConsole.WriteLine ("Alarm:{0}-{1}:", Heater.area, Heater.type); Console.WriteLine ("Alarm: Beep, the water has been {0} degrees:", e.temperature); Console.WriteLine (); } } //Display Public classDisplay { Public Static voidshowmsg (Object Sender,boiledeventargs e) {//Static Methodsheared heater =(heared) sender; Console.WriteLine ("Display:{0}-{1}:", Heater.area, Heater.type); Console.WriteLine ("Display: The water is burning fast, the current temperature: {0} degrees. ", e.temperature); Console.WriteLine (); } } classProgram {Static voidMain () {heared heater=Newheared (); Alarm Alarm=NewAlarm (); Heater. Boidevent+ = alarm. Makealert;//Registration MethodHeater. Boidevent + = (NewAlarm ()). Makealert;//registering methods for anonymous objectsHeater. Boidevent + =NewHeared.boidhandler (alarm. Makealert);//You can also register like thisHeater. Boidevent + = display.showmsg;//registering a static methodheater. Boilwater (); //boil water and automatically invoke the method that registered the object } }#endregion
The output is:
Alarm:china Xian-realfire 001:
Alarm: Beep, the water has been 96 degrees:
Alarm:china Xian-realfire 001:
Alarm: Beep, the water has been 96 degrees:
Alarm:china Xian-realfire 001:
Alarm: Beep, the water has been 96 degrees:
Display:china Xian-realfire 001:
Display: The water is burning fast, the current temperature: 96 degrees.
Omitted...
The observer design pattern consists of the following two types of objects:
- Subject: A monitoring object that often contains content that other objects are interested in. In this example, the water heater is a monitoring object that contains other objects that are of interest to the Temprature field, and when the value of this field is as fast as 100, the data is constantly sent to the object that monitors it.
- Observer: The Watcher, which monitors subject, notifies Observer when something happens in subject, and Observer takes action accordingly. In this example, the observer has alarms and displays, and the actions they take are to issue alarms and display the water temperature respectively.
In this case, the order in which things should happen is this:
- Alarms and monitors tell the water heater that it is interested in its temperature (registration).
- The water heater knows to keep a reference to the siren and monitor.
- Water heater This action, when the water temperature is more than 95 degrees, through the alarm and display reference, automatically invoke the alarm Makealert () method, the display of the ShowMsg () method.
Examples like this are many, and Gof is abstracting it, called Observer design pattern: Theobserver design pattern is to define a one-to-many dependency between objects so that when the state of an object changes, other objects that depend on it are automatically notified and updated. Observer mode is a loosely coupled design pattern.
Coding specifications for the. Net Framework:
- The name of the delegate type should end with EventHandler.
- Prototype definition of a delegate: there is a void return value and accepts two input parameters: an object type, a EventArgs type (or inherited from EventArgs).
- The name of the event is the remainder of the delegate after the EventHandler is removed.
- Types that inherit from EventArgs should end with EventArgs.
Let's do another explanation:
- The parameter of type object in the delegate declaration prototype represents the subject, which is the monitoring object, in this case the heated (water heater). A callback function (such as alarm's Makealert) can access the object that triggered the event (heated).
- The EventArgs object contains the data that observer is interested in, in this case the temperature.
C # A Few good examples of events