日期:2014-05-18  浏览次数:20810 次

初学C#求指导、求优化、求砖。
今天看了“张子阳”的《C#委托和事件》教程,学着做了一个探头监控报警程序。
使用了Observer模式,但里面还是有不少的if判断,求各路神仙指点。谢谢!!

C# code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Timers;

namespace DetectorAlarm
{
    /// <summary>
    /// 温湿度报警程序
    /// </summary>
    class Program
    {
        static void Main(string[] args)
        {
            Detector detector = new Detector();
            SMSAlert sms = new SMSAlert();
            DeviceAlert device = new DeviceAlert();
            ServerAlert server = new ServerAlert();

            detector.MonitorFaild += sms.SendSMS;
            detector.MonitorFaild += device.DoAlert;
            detector.MonitorFaild += server.DoAlert;

            detector.DoMonitor();
            Console.ReadLine();

        }
    }
    /// <summary>
    /// 枚举类型
    /// </summary>
    enum DetectorEventType
    {
        Normal = 0,
        TemperatureHigh = 2,
        TemperatureLow = 4,
        HumidityHigh = 8,
        HumidityLow= 16      
    }
    /// <summary>
    /// 探头对象
    /// </summary>
    class Detector
    {
        #region 内部成员
        //探头参数:绑定的设备号,设备IP,通道号,楼层,所在地点
        private int _detectorID = 0;
        private int _deviceNo;
        private string _deviceIP;
        private int _channel;
        private string _floor;
        private string _place;

        //内部参数:温/湿度当前值,报警上下限
        private double _temperature;
        private double _humidity;
        private double _minTemperature;
        private double _maxTemperature;
        private double _minHumidity;
        private double _maxHumidity;
        #endregion

        #region 只读属性
        public int DeviceNo
        {
            get { return _deviceNo; }
        }
        public string DeviceIP
        {
            get { return _deviceIP; }
        }

        public int Channel
        {
            get { return _channel; }
        }
        public double Temperature
        {
            get { return _temperature; }
        }
        public double Humidity
        {
            get { return _humidity; }
        }
        #endregion

        #region 事件与委托定义
        //声明委托
        public delegate void MonitorEventHandler(Object sender, MonitorEventArgs e);
        //声明事件
        public event MonitorEventHandler MonitorFaild;
        /// <summary>
        /// MonitorEventArgs类,用于传递参数
        /// </summary>
        public class MonitorEventArgs:EventArgs
        {
            public readonly double temperature;
            public readonly double humidity;
            public readonly int eventType;
            public MonitorEventArgs(int eventType, double temperature, double humidity)
            {
                this.eventType = eventType;
                this.temperature = temperature;
                this.humidity = humidity;
            }
        }
        #endregion

        #region 自定义事件
        /// <summary>
        /// 温度过高
        /// </summary>
        /// <param name="e"></param>
        protected virtual void OnMonitorFaild(MonitorEventArgs e)
        {
            if (MonitorFaild != null)
            {
                MonitorFaild(this, e);
            }
        }
        #endregion

        #region 公共方法
        public void DoMonitor()
        {
            //从数据库读取探头信息
            if(_detectorID==0)
            {
                GetDetectorInfo();
            }
            System.Timers.Timer timer = new System.Timers.Timer();
            timer.Interval = 6000;
            timer.Enabled = true;
            timer.Elapsed += new ElapsedEventHandler(GetDetectorInfoByID);
            timer.Start();
        }
        #endregion

        #region 私有方法
        /// <summary>
        /// 获取探头信息
        /// </summary>