Head First Design Patterns - observer mode

1, weather monitoring applications, error demonstration

There is a weather station, there are three means: the temperature sensing means, the humidity sensing means, pressure sensing means. WeathData weather object tracking data, there WeathData MeasurmentsChanged () method, after the data change sensing device is called MeasurmentsChanged. Currently demand is three to bulletin boards, namely meteorological data status (CurrentConditionDisply), weather statistics (StaisticsDisply), weather forecasts (ForcastDisply) used to display a variety of data. According to this demand we can design follows

public class WeatherData(){
    private float Temperature{get;set;}
    private float Humidity{get;set;}
    private float Pressure{get;set;}

    public void MeasurmentsChanged(){
        CurrentConditionDisply.Update(Temperature,Humidity,Pressure);
        StaisticsDisply.Update(Temperature,Humidity,Pressure);
        ForcastDisply.Update(Temperature,Humidity,Pressure);
    }
}

public class CurrentConditionDisply{
    public void Update(float temperature,float humidity,float Pressure){
        //更新公布数据
    }
}
public class StaisticsDisply{
    public void Update(float temperature,float humidity,float Pressure){
        // update the statistics 
    } 
}
public class ForcastDisply{
    public void the Update (a float temperature, humidity a float, a float Pressure) { 
        // update the weather forecast 
    } 
}

According to the above designs can also realize the current demand, but if the new addition of a bulletin board or delete a bulletin board, then we need to seek change MeasurmentsChanged method.

This example of problems caused by:

1, we are for the realization of programming, rather than fight for the interface.

2, for each new bulletin board, we had to modify the code.

3, can not dynamically add or remove bulletin boards in operation.

4, changing the unencapsulated portion violation closed for modification, the extended open.

2, the use of the observer pattern decouple

Observer Pattern: Defines the many dependencies between objects, when an object is changed, all dependent on his will be notified and updated automatically.

Subscribe to the newspaper is a typical observer mode, Press is the theme (subject), that is, subscribers observer (observer), when a new newspaper, newspaper readers to send a new newspaper will be sent to subscribe to the newspaper's hands . Subscription model is given here to the class diagram, then we weather stations prior to encapsulation mode for the observer.

3, the observer using the improved weather patterns

Designed according to the observer and improved weather patterns and test the code

    /// <summary>
    /// 主题
    /// </summary>
    public interface Subject
    {
        public void RegisterObserver(Observer o);
        public void RemoveObserver(Observer o);
        public void NotifyObserver();
    }
    /// <summary>
    /// 具体主题(气象站)
    /// </summary>
    public class WeatherData : Subject
    {
        private List<Observer> observers;
        private float Temperature { get; set; }
        private float Humidity { get; set; }
        private float Pressure { get; set; }

        public WeatherData()
        {
            observers = new List<Observer>();
        }
        public void RegisterObserver(Observer o)
        {
            observers.Add(o);
        }

        public void RemoveObserver(Observer o)
        {
            observers.Remove(o);
        }

        //通知观察者
        public void NotifyObserver()
        {
            foreach (var o in observers)
            {
                o.Update(Temperature, Humidity, Pressure);
            }
        }

        public void MeasurmentsChanged()
        {
            NotifyObserver();
        }

        //数据变化
        public void SetMeasurments(float temperature, float humidity, float pressure)
        {
            Temperature = temperature;
            Humidity = humidity;
            Pressure = pressure;
            MeasurmentsChanged();
        }
    }

    /// <summary>
    /// 订阅者
    /// </summary>
    public interface Observer
    {
        void Update(float temperature, float humidity, float pressure);
    }

    public class CurrentConditionDisply : Observer
    {
        private Subject weatherData;
        public CurrentConditionDisply(Subject weatherData)
        {
            this.weatherData = weatherData;
            weatherData.RegisterObserver(this);
        }
        public void Update(float temperature, float humidity, float pressure)
        {
            Console.WriteLine($"当前情况布告板:{temperature},{humidity},{pressure}");
        }
    }
    public class StaisticsDisply : Observer
    {
        private Subject weatherData;
        public StaisticsDisply(Subject weatherData)
        {
            this.weatherData = weatherData;
            weatherData.RegisterObserver(this);
        }
        public void Update(float temperature, float humidity, float pressure)
        {
            Console.WriteLine($"统计数据布告板:{temperature},{humidity},{pressure}");
        }
    }
    public class ForcastDisply : Observer
    {
        private Subject weatherData;
        public ForcastDisply(Subject weatherData)
        {
            this.weatherData = weatherData;
            weatherData.RegisterObserver(this);
        }
        public void Update(float temperature, float humidity, float pressure)
        {
            Console.WriteLine($"天气预报布告板:{temperature},{humidity},{pressure}");
        }
    }

Test Results:

Guess you like

Origin www.cnblogs.com/SunSpring/p/11717399.html