Event-driven architecture (EDA) and Observer Model

Source: Internet
Author: User

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

 
 
  1. public delegate int TestDelegate(object obj1, object obj2) ; 

Instantiate a delegate

 
 
  1. TestDelegate TD = new TestDelegate(TestDelegateMethod) ; 

Call a delegate

 
 
  1. 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:

 
 
  1. public delegate void DoAction(int s); 
  2.     class BloodPressureMonitor 
  3.     { 
  4.         public void CheckPresssure(int p) 
  5.         { 
  6.             if (p < 80) 
  7.             { 
  8.                 Console.WriteLine(String.Format("Alert!Bloodpressure is less than {0}", p)); 
  9.             } 
  10.             else 
  11.             { 
  12.                 Console.WriteLine(String.Format("Bloodpressure is {0}", p)); 
  13.             } 
  14.         } 
  15.     } 
  16.     class Patient { 
  17.         public event DoAction dosth; 
  18.         public String Name { get; set; } 
  19.         public int BloodPressure { get; set; } 
  20.         public void HeartBeat() 
  21.         { 
  22.             if (dosth != null) 
  23.                 dosth(BloodPressure); 
  24.         } 
  25.     } 
  26.     class Program 
  27.     { 
  28.         static void Main(string[] args) 
  29.         { 
  30.             Patient  p = new Patient(); 
  31.             p.Name = "Tom"; 
  32.             p.BloodPressure = 100; 
  33.             BloodPressureMonitor monitor = new BloodPressureMonitor(); 
  34.             p.dosth += monitor.CheckPresssure; 
  35.             for (int i = 0; i < 10; i++) 
  36.             { 
  37.                 p.BloodPressure -= 5; 
  38.                 p.HeartBeat(); 
  39.             } 
  40.         } 
  41.     } 

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

 
 
  1. public void HeartBeat() 
  2.  { 
  3.      new BloodPressureMonitor(). CheckPresssure( BloodPressure ); 
  4.  } 

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:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Linq;
  4. Using System. Text;
  5. Namespace EDA
  6. {
  7. Public delegate void DoAction (int s); // defines a delegate
  8. Class BloodPressureMonitor
  9. {
  10. Public void CheckPresssure (int p)
  11. {
  12. If (p <80)
  13. {
  14. Console. WriteLine (String. Format ("Alert! Bloodpressure is less than {0} ", p ));
  15. }
  16. Else
  17. {
  18. Console. WriteLine (String. Format ("Bloodpressure is {0}", p ));
  19. }
  20. }
  21. }
  22. Class Telephone
  23. {
  24. Public void NeedCallDoctor (int p)
  25. {
  26. If (p <60)
  27. {
  28. Console. WriteLine (String. Format ("Call doctor! ", P ));
  29. }
  30. }
  31. }
  32. Class Patient
  33. {
  34. Public event DoAction dosomething;
  35. Public String Name {get; set ;}
  36. Public int BloodPressure {get; set ;}
  37. Public void HeartBeat () // specifies the HeartBeat
  38. {
  39. If (dosomething! = Null)
  40. Dosomething (BloodPressure );
  41. }
  42. }
  43. Class Program
  44. {
  45. Static void Main (string [] args)
  46. {
  47. Patient p = new Patient ();
  48. P. Name = "Tom ";
  49. P. blomaxpressure = 100;
  50. BloodPressureMonitor monitor = new BloodPressureMonitor ();
  51. P. dosomething + = monitor. CheckPresssure; // you can specify the method used to set dosomething. You can attach multiple methods with ++ =.
  52. Telephone Phone = new Telephone ();
  53. P. dosomething + = Phone. NeedCallDoctor;
  54. For (int I = 0; I <10; I ++)
  55. {
  56. P. BloodPressure-= 5;
  57. P. HeartBeat ();
  58. }
  59. }
  60. }
  61. }

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

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.