2009-11-26 87 views
3

我想要一个事件通知系统,当病人的心跳超过120时应该通知医生。我不知道,如何设计这样的系统。只是我实施了错误的一个。帮助我实现正确的一个。C# - 事件设计注意事项

 static void Main() 
    { 
    Patient[] patList = { new Patient 
    { PatientID = "1", HeartBeat = 100 }, 
     new Patient { PatientID = "2", HeartBeat = 130 } }; 

     List<Patient> plist = patList.ToList(); 
     Console.ReadKey(true); 
    } 


public class Doctor 
    { 
     public event PulseNotifier AbnormalPulseRaised; 
     public string Name 
     { 
      get; 
      set; 
     } 
    } 



public class Patient 
    { 
     public event PulseNotifier AbnormalPulseRaised; 
     static Random rnd = new Random(); 

     public Patient() 
     { 
      PulseNotifier += new PulseNotifier(OnAbnormalPulseRaised); 
     } 
     public string PatientID 
     { 
      get; 
      set; 
     } 

     public int HeartBeat 
     { 
      get; 
      set; 
     } 

     public void HeartBeatSimulation(List<Patient> patList) 
     { 
      foreach(Patient p in patList) 
      { 
       if (p.HeartBeat > 120) 
       { 
        if (AbnormalPulseRaised != null) 
        { 
         AbnormalPulseRaised(p); 
        } 
       } 
      } 
     } 

     public void OnAbnormalPulseRaised(Patient p) 
     { 
      Console.WriteLine("Patient Id :{0},Heart beat {1}", 
      p.PatientID, p.HeartBeat); 
     } 
    } 

除此之外,我想有一个共同的说明。

记住发布者和观察者模式的最佳方式是什么?因为我很困惑在哪里实现发布者和在哪里实施

回答

3

那么,对于初学者来说,我通常认为如果你有权访问同一个班级的班级的事件,这是一个糟糕的想法。

从MS推荐的EventArgs派生也是一个好主意。

提高事件的责任实际上应该在患者类中,但是在这里您只提出了您调用HardBeatSimulation函数本身的类的事件,而不是实际上具有异常脓肿的患者:)

static void Main(string[] args) { 
     Patient pat1 = new Patient(1, 120); 
     Patient pat2 = new Patient(3, 150); // this one can have a 150 bpm hartbeat :) 
     Doctor fancyDoctor = new Doctor(); 
     fancyDoctor.AddPatient(pat1); 
     fancyDoctor.AddPatient(pat2); 
     Console.ReadKey(true); 

    } 

    public class Doctor { 
     List<Patient> _patients; 
     public event EventHandler Working; 


     public Doctor() { 
      _patients = new List<Patient>(); 
     } 

     public void AddPatient(Patient p) { 
      _patients.Add(p); 
      p.AbnormalPulses += new EventHandler<AbnormalPulseEventArgs>(p_AbnormalPulses); 
     } 

     void p_AbnormalPulses(object sender, AbnormalPulseEventArgs e) { 
      OnWorking(); 
      Console.WriteLine("Doctor: Oops, a patient has some strange pulse, giving some valium..."); 
     } 

     protected virtual void OnWorking() { 
      if (Working != null) { 
       Working(this, EventArgs.Empty); 
      } 
     } 

     public void RemovePatient(Patient p) { 
      _patients.Remove(p); 
      p.AbnormalPulses -= new EventHandler<AbnormalPulseEventArgs>(p_AbnormalPulses); 
     } 
    } 

    public class Patient { 
     public event EventHandler<AbnormalPulseEventArgs> AbnormalPulses; 

     static Random rnd = new Random(); 
     System.Threading.Timer _puseTmr; 
     int _hartBeat; 

     public int HartBeat { 
      get { return _hartBeat; } 
      set { 
       _hartBeat = value; 
       if (_hartBeat > MaxHartBeat) { 
        OnAbnormalPulses(_hartBeat); 
       } 
      } 
     } 

     protected virtual void OnAbnormalPulses(int _hartBeat) { 
      Console.WriteLine(string.Format("Abnormal pulsecount ({0}) for patient {1}", _hartBeat, PatientID)); 
      if (AbnormalPulses != null) { 
       AbnormalPulses(this, new AbnormalPulseEventArgs(_hartBeat)); 
      } 
     } 

     public Patient(int patientId, int maxHartBeat) { 
      PatientID = patientId; 
      MaxHartBeat = maxHartBeat; 
      _puseTmr = new System.Threading.Timer(_puseTmr_Tick); 

      _puseTmr.Change(0, 1000); 
     } 

     void _puseTmr_Tick(object state) { 
      HartBeat = rnd.Next(30, 230); 
     } 

     public int PatientID { 
      get; 
      set; 
     } 

     public int MaxHartBeat { 
      get; 
      set; 
     } 
    } 

    public class AbnormalPulseEventArgs : EventArgs { 
     public int Pulses { get; private set; } 
     public AbnormalPulseEventArgs(int pulses) { 
      Pulses = pulses; 
     } 
    } 
+0

@Stormenet未引发事件。 – user215675 2009-11-26 10:32:22

+0

好吧,它现在可以工作,因为没有应用程序循环,所以需要使用System.Threading.Timer而不是System.Windows.Forms.Timer :) – Stormenet 2009-11-26 11:01:17

2

方法OnAbnormalPulseRaised(Patient p)应该放在Doctor类中,因为医生是被通知事件的人。该事件属性应放置在患者的类,因为病人都在提高事件:

public class Doctor 
{ 
     public Doctor() 
     { 
      // doctor initialization - iterate through all patients 
      foreach(patient in patList) 
      { 
       // for each patient register local method as event handler 
       // of the AbnormalPulseRaised event. 
       patient.AbnormalPulseRaised += 
        new PulseNotifier(this.OnAbnormalPulseRaised); 

      } 
     } 

     public void OnAbnormalPulseRaised(Patient p) 
     { 
      Console.WriteLine("Patient Id :{0},Heart beat {1}", 
      p.PatientID, p.HeartBeat); 
     } 


     public string Name 
     { 
      get; 
      set; 
     } 
} 

public class Patient 
{ 
     public event PulseNotifier AbnormalPulseRaised; 
     static Random rnd = new Random(); 

     public Patient() 
     { 
     } 

     public string PatientID 
     { 
      get; 
      set; 
     } 

     public int HeartBeat 
     { 
      get; 
      set; 
     } 

     public void HeartBeatSimulation(List<Patient> patList) 
     { 
      foreach(Patient p in patList) 
      { 
       if (p.HeartBeat > 120) 
       { 
        if (AbnormalPulseRaised != null) 
        { 
         AbnormalPulseRaised(p); 
        } 
       } 
      } 
     } 
    } 

出版商与事件属性的对象。订阅者是具有处理程序方法的对象。