Example #1
0
    static void Main(string[] args)
    {
        /// Create the observable(weather station)
        WeatherStation station = new WeatherStation();

        /// Create 2 observers
        WeatherDataPrinter    printer    = new WeatherDataPrinter();
        WeatherDataAggregator aggregator = new WeatherDataAggregator();

        /// Add the observers
        station.AddObserver(printer);
        station.AddObserver(aggregator);

        for (int i = 10; i <= 40; ++i)
        {
            station.AddData(new WeatherData("Temperature", i));
        }

        Console.WriteLine("The final average is " + aggregator.getAvg());
    }
Example #2
0
    public static void Main(string[] args)
    {
        // create the observable
        WeatherStation station = new WeatherStation();

        // create the observers
        WeatherDataPrinter    printer    = new WeatherDataPrinter();
        WeatherDataAggregator aggregator = new WeatherDataAggregator();

        var subscriber1 = station.Subscribe(printer);
        var subscriber2 = station.Subscribe(aggregator);

        station.AddData(new WeatherData("Temperature", 100));
        station.AddData(new WeatherData("Temperature", 80));

        // subscriber1.Dispose();
        station.PrintSubscribers();

        Console.WriteLine(aggregator.getAverage());
    }