Event-Driven Architecture (EDA) is a low-coupling and distributed Architecture that processes asynchronous information flows.
Generally, events can be seen as a change in the business field. Therefore, we need to define an event in the Code and instantiate it. In. net, events can be viewed as results of actions. There must be an event producer and receiver. The event trigger object is the producer, and the event response object is the event receiver. Delegate) is connected to the sender and receiver. The concept of delegation in C # is not described here. The following Code declares the delegate, initializes the delegate, and calls the delegate.
Declare a delegate
- public delegate int TestDelegate(object obj1, object obj2) ;
Instantiate a delegate
- TestDelegate TD = new TestDelegate(TestDelegateMethod) ;
Call a delegate
- TestDelegateMethod(" This is a Test.");
Note that in C #, most of the time is the delegate modified by event. The delegate modified by event is a Special Delegate. Its particularity lies in the encapsulation of objects. For example, the above Code can be written
Event TestDelegate TD = TestDelegateMethod; for more information about event and delegate, see blog
Http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx
Http://blog.monstuff.com/archives/000040.html
Assume that, in the next scenario, a blood pressure monitoring instrument monitors blood pressure at each heartbeat of a patient. Once the patient's blood pressure is found to be less than 80, an alarm is triggered.
In this case, the sender of the action is the patient. Once the heartbeat action is triggered, the blood pressure detector immediately detects the blood pressure. If it is smaller than 80, an alarm is triggered.
There is a BloodPressureMonitor class and an upload ent class. The BloodPressureMonitor class only uses one method to monitor blood pressure. The diseased human mainly has a delegate and a HeartBeat () method. Each time the HeartBeat () method is called, the delegate method is called. Here, the meaning of delegation is similar to the concept of functional programming. A function is used as a variable value, and the variable is assigned to the function that calls the function ).
Therefore, in the main method, it is mainly to assign values to the doth delegate, that is, to call the CheckPresssure method of the blood pressure detector every time the patient's heartbeat.
The Code is as follows:
- public delegate void DoAction(int s);
- class BloodPressureMonitor
- {
- public void CheckPresssure(int p)
- {
- if (p < 80)
- {
- Console.WriteLine(String.Format("Alert!Bloodpressure is less than {0}", p));
- }
- else
- {
- Console.WriteLine(String.Format("Bloodpressure is {0}", p));
- }
- }
- }
- class Patient {
- public event DoAction dosth;
- public String Name { get; set; }
- public int BloodPressure { get; set; }
- public void HeartBeat()
- {
- if (dosth != null)
- dosth(BloodPressure);
- }
- }
- class Program
- {
- static void Main(string[] args)
- {
- Patient p = new Patient();
- p.Name = "Tom";
- p.BloodPressure = 100;
- BloodPressureMonitor monitor = new BloodPressureMonitor();
- p.dosth += monitor.CheckPresssure;
- for (int i = 0; i < 10; i++)
- {
- p.BloodPressure -= 5;
- p.HeartBeat();
- }
- }
- }
The running result is as follows:
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" inherit "border =" 0 "alt =" 600A4D7F7AA747EBA23D5B30A8C0B3D2 "src =" http://www.bkjia.com/uploads/allimg/131228/16245441W-0.jpg "height =" 212 "/>
Many may think that, why bother calling the CheckPresssure method of BloodPressureMonitor In the HeartBeat method? For example, directly write
- public void HeartBeat()
- {
- new BloodPressureMonitor(). CheckPresssure( BloodPressure );
- }
Can this method be implemented?
Yes, but this writing method is not flexible. First, you can use some characteristics of the Delegate, such as calling the delegate's BeginInvoke method can be executed asynchronously on the thread pool thread.
Next, if you need to add a call function at this time, once the blood pressure is less than 60, the telephone will automatically call the doctor, then it is very easy to change based on the existing. You only need to add a telephone class, and then mount another method on the delegate.
The Code is as follows:
- Using System;
- Using System. Collections. Generic;
- Using System. Linq;
- Using System. Text;
- Namespace EDA
- {
- Public delegate void DoAction (int s); // defines a delegate
- Class BloodPressureMonitor
- {
- Public void CheckPresssure (int p)
- {
- If (p <80)
- {
- Console. WriteLine (String. Format ("Alert! Bloodpressure is less than {0} ", p ));
- }
- Else
- {
- Console. WriteLine (String. Format ("Bloodpressure is {0}", p ));
- }
- }
- }
- Class Telephone
- {
- Public void NeedCallDoctor (int p)
- {
- If (p <60)
- {
- Console. WriteLine (String. Format ("Call doctor! ", P ));
- }
- }
- }
- Class Patient
- {
- Public event DoAction dosomething;
- Public String Name {get; set ;}
- Public int BloodPressure {get; set ;}
- Public void HeartBeat () // specifies the HeartBeat
- {
- If (dosomething! = Null)
- Dosomething (BloodPressure );
- }
- }
- Class Program
- {
- Static void Main (string [] args)
- {
- Patient p = new Patient ();
- P. Name = "Tom ";
- P. blomaxpressure = 100;
- BloodPressureMonitor monitor = new BloodPressureMonitor ();
- P. dosomething + = monitor. CheckPresssure; // you can specify the method used to set dosomething. You can attach multiple methods with ++ =.
- Telephone Phone = new Telephone ();
- P. dosomething + = Phone. NeedCallDoctor;
- For (int I = 0; I <10; I ++)
- {
- P. BloodPressure-= 5;
- P. HeartBeat ();
- }
- }
- }
- }
650) this. width = 650; "style =" border-bottom: 0px; border-left: 0px; border-top: 0px; border-right: 0px "title =" inherit "border =" 0 "alt =" 9C986808A9BF457CA9576779A0976D98 "src =" http://www.bkjia.com/uploads/allimg/131228/162454H64-1.jpg "height =" 255 "/>
This article is from the "one blog" blog, please be sure to keep this source http://cnn237111.blog.51cto.com/2359144/1176649