Observer Pattern in C# Revisited

In the last post, @jerry made some good comments for cleaning up “visual noise”. The code below fixes that and also removes the delegate:

    public class WeatherDataEventArgs : EventArgs
    {
        public float Temperature { get; set; }

        public float Humidity { get; set; }

        public float Pressure { get; set; }

        public WeatherDataEventArgs(float temperature, float humidity, float pressure)
        {
            this.Temperature = temperature;
            this.Humidity = humidity;
            this.Pressure = pressure;
        }
    }

    public class WeatherData
    {
        public event EventHandler<WeatherDataEventArgs> MeasurementsChanged;

        public void SetMeasurements(float temperature, float humidity, float pressure)
        {
            WeatherDataEventArgs weatherArgs = new WeatherDataEventArgs(temperature, humidity, pressure);

            MeasurementsChanged(this, weatherArgs);
        }
    }

The delegate is not actually needed due to the EventHandler<T> delegate in the CLR. Saves a line. This is now a bit more concise.

I then fixed up the StatisticsDisplay class to remove the List<T>. That was kinda dumb.  The new code is below.  It’s more verbose, but more performant.

    class StatisticsDisplay : IDisplayElement
    {
        private WeatherData _weatherData;
        private float _max;
        private float _min;
        private int _samples;
        private float _sum;

        public StatisticsDisplay(WeatherData weatherData)
        {
            this._max = 0;
            this._min = 0;
            this._samples = 0;
            this._sum = 0;
            this._weatherData = weatherData;
            weatherData.MeasurementsChanged += Update;
        }

        private void ProcessData(float temperature)
        {
            this._samples++;
            this._sum += temperature;

            this._max = Math.Max(temperature, this._max);
            this._min = Math.Min(temperature, this._min);
        }

        public void Update(object sender, WeatherDataEventArgs we)
        {
            ProcessData(we.Temperature);
            Display();
        }

        public void Display()
        {
            String output;

            if (this._samples == 0)
            {
                output = "No data retrieved yet.";
            }
            else
            {
                output = String.Format("Avg/Max/Min temperature = {0}/{1}/{2}", (this._sum / this._samples), this._max, this._min);
            }
            Console.WriteLine(output);
        }
    }

This looks a lot better. 🙂


Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

2 Responses to Observer Pattern in C# Revisited

  1. jerry says:

    suggestion: _max = Math.Max(temperature, _max)

    and unless you’re working in Kelvin, 0 is not a good initial value. Someone in Antarctica might be using this. Your safe because of the “first sample” check, but might consider using float.MaxValue and float.MinValue, then no need to check for first sample.

    Also, I pity the fool who calls Display() before the first sample has arrived 🙂

    • nitdoggx says:

      @jerry Fixed per your suggestions, except for the 0 initialization. Even if it were in Kelvin and/or you were in Antarctica, you might freak out if Display showed you -3.402823e38 degrees. That’s cold.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s