Publish subscriber Mode C # delegate implementation

Source: Internet
Author: User
1 using system; 2 using system. collections. generic; 3 using system. LINQ; 4 using system. text; 5 using system. threading. tasks; 6 7 namespace delegate and publish subscriber Mode 8 {9 10/** 11 * machine as subscriber, repair worker repairer as subscriber 12 * worker (subscriber) register different machine events, register the callback function to handle 13 * different workers when the corresponding event occurs. only pay attention to the events they are interested in. 14 */15 // machine Class 16 public class machine 17 {18 // whether the machine is working normally 19 public bool isworkwell {Get; set;} 20 21 // host fault event Delegate 22 public de Legate void machinebroken (string brokenmsg); 23 public event machinebroken machinebrokenevent; 24 25 // 26 Public Delegate void machineworkwell (string MSG); 27 public event machineworkwell machineworkwellevent; 28 29 // machine operation 30 public void run () 31 {32 isworkwell = false; // set machine failure 33 34 if (machinebrokenevent! = NULL) // a subscriber registers for a Machine Fault event 35 {36 IF (! Isworkwell) 37 machinebrokenevent ("the machine has failed. Let's take a look at the speed ~ "); 38} 39 40 isworkwell = true; // set the machine to restore to normal 41 42 if (machineworkwellevent! = NULL) // There Is A subscriber registered machine running normal event 43 {44 If (isworkwell) 45 machineworkwellevent ("the machine is running properly, don't worry, kiss ~ "); 46} 47} 48 49 // public void onmachinebrokenevent (string brokenmsg) 50 // {51 // machinebroken handler = machinebrokenevent; 52 // If (handler! = NULL) handler (brokenmsg); 53 //} 54 55 // public void onmachineworkwellevent (string MSG) 56 // {57 // machineworkwell handler = machineworkwellevent; 58 // If (handler! = NULL) handler (MSG); 59 //} 60} 61 62 public interface iworker 63 {64 string name {Get; set;} 65 void dojob (string MSG ); 66} 67 68 // busyworker registers for a machine failure event 69 public class busyworker: iworker 70 {71 public string name {Get; set;} 72 73 public void dojob (string MSG) 74 {75 console. writeline (string. format ("{0}: Received message <{1}>! Please wait ~ ", Name, MSG); 76} 77} 78 79 // idleworker registers for normal machine operation event 80 public class idleworker: iworker 81 {82 public string name {Get; set ;} 83 84 public void dojob (string MSG) 85 {86 console. writeline (string. format ("{0}: Received message <{1}>! Very good, so that I can have more sleep ~ ", Name, MSG); 87} 88} 89 90 public class subscriberpatter=adelegate 91 {92 static void main (string [] ARGs) 93 {94 machine = new machine (); 95 96 iworker busyworker = new busyworker (); 97 busyworker. name = "busy worker"; 98 99 iworker idleworker = new idleworker (); 100 idleworker. name = "Idle worker"; 101 102 machine. machinebrokenevent + = new machine. machinebroken (busyworker. dojob); 103 machine. machineworkwellevent + = new machine. machineworkwell (idleworker. dojob ); 104 105 ////////////////////////////////////// //////////////////////////////////////// /////////// 106 // verify whether multiple subscribers can be added for an event 107 iworker busyworker2 = new busyworker (); 108 busyworker2.name = "busy worker 2"; 109 machine. machinebrokenevent + = new machine. machinebroken (busyworker2.dojob); 110 // the test result is 111. // you can also use static delegation, one of the advantages of static delegation is that it saves 112 of the memory space ///////////////////////////// //////////////////////////////////////// /// // 113 114 machine. run (); // The machine starts to operate 115 116 console. readline (); 117} 118} 119}

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.