Observer of design patterns-delegation and events

Source: Internet
Author: User

Observer of design patterns-delegation and events
Observer mode (Observer)
Defines a one-to-many dependency, allowing multiple observer objects to listen to a topic object at the same time. When the status of this topic object changes, it notifies all observer objects so that they can automatically update themselves.



Suppose there is a high-end water heater, when the water temperature exceeds 95 degrees: 1. The speaker will start to give a voice to tell you the water temperature; 2. the LCD screen will also change the display of the water temperature, to prompt that the water is almost boiling.


If we want to simulate the process of boiling water through a program. It is assumed that the water heater is composed of three parts: water heater, alarm, and display, which are from different manufacturers and assembled. Then, the water heater should only be responsible for boiling water, it cannot issue an alarm or display the water temperature; when the water is open, the alarm is issued by the alarm, the display shows the water temperature.

Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; namespace delegate {// public class Heater {private int temperature; public string type = "RealFire 001"; // Add a model as demonstration public string area = "China Xian "; // Add origin as demo // declare delegate public delegate void BoiledEventHandler (object sender, BoiledEventArgs e); public event BoiledEventHandler Boiled; // declare event // set The BoiledEventArgs class is passed to the Observer for information of interest. public class BoiledEventArgs: EventArgs {public readonly int temperature; public BoiledEventArgs (int temperature) {this. temperature = temperature; }}// you can rewrite the class that is integrated from Heater so that the inherited class rejects monitoring of other objects. protected virtual void OnBoiled (BoiledEventArgs e) {if (Boiled! = Null) {Boiled (this, e); // call the methods of all registered objects} // boil public void BoilWater () {for (int I = 0; I <= 100; I ++) {temperature = I; if (temperature> 95) {// create the BoiledEventArgs object BoiledEventArgs e = new BoiledEventArgs (temperature); OnBoiled (e ); // call the OnBolied method }}}// alert public class Alarm {public void MakeAlert (object sender, Heater. boiledEventArgs e) {Heater heater = (Heater) sender; // access the public field C in sender Onsole. writeLine ("Alarm: {0}-{1}:", heater. area, heater. type); Console. writeLine ("Alarm: drib, water already {0};", e. temperature); Console. writeLine () ;}// Display public class Display {public static void ShowMsg (object sender, Heater. boiledEventArgs e) {Heater heater = (Heater) sender; Console. writeLine ("Display: {0}-{1}:", heater. area, heater. type); Console. writeLine ("Display: The water is almost burned out. Current temperature: {0. ", E. temperature); Console. writeLine () ;}} class Program {static void Main (string [] args) {Heater heater = new Heater (); Alarm alarm = new Alarm (); heater. boiled + = alarm. makeAlert; // registration method heater. boiled + = Display. showMsg; // register the static method heater. boilWater (); // The method for registering an object is automatically called }}}




Class diagram:
The difficulty of system design.
3. The observer mode meets the requirements of the "open and closed principle" and can dynamically Add the observer object at runtime.


Applicable scenarios:
1. When an abstract model has two aspects, one of which is dependent on the other, it needs to encapsulate the two aspects into independent objects and change and reuse each other independently.
2. When an object in a system needs to change the content of other objects at the same time, but you do not know the number of objects to be changed.
3. When an object changes, it must notify other objects to make corresponding changes, but cannot determine who the notification object is.


Related mode:
1. intermediary: In the intermediary, Notification status changes are only part of the pattern. The focus of this pattern is to coordinate collaboration between various colleagues' classes.
2. Observer: when the topic changes, the observer receives the notification and synchronizes with the topic status.



Brief delegation:

1. A delegate is a class that defines the type of a method so that the method can be passed as a parameter of another method. In this way, the method is dynamically assigned to the parameter, it can avoid using the If-Else (Switch) statement in a large number in the program, and make the program more scalable.
2. you can use a delegate to bind multiple methods to the same delegate variable. When you call this variable (the word "call" is used here, because this variable represents a method ), you can call all bound methods at a time.

. NET Framework encoding Specification
1. All delegate type names should end with EventHandler
2. the prototype of the delegate has a void returned value and accepts two input parameters: an Object type and an EventArgs type (or inherited from EventArgs)
3. The event is named as the remaining part after commissioned EventHandler.
4. The type inherited from EventArgs should end with EventArgs








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.