Esempio n. 1
0
        // 2. Observer Pattern
        private static void RunObserverPatternDemo()
        {
            WeatherPublisher weatherPublisher        = new WeatherPublisher();
            ISubscriber      currentConditionDisplay = new CurrentConditionsDisplay(weatherPublisher);
            ISubscriber      statisticalDisplay      = new StatisticalDisplay(weatherPublisher);

            Console.WriteLine("Set Weather the first time");
            weatherPublisher.SetWeatherStats(0, 0, 0);
            Console.WriteLine();

            Console.WriteLine("Set Weather the second time");
            weatherPublisher.SetWeatherStats(1, 1, 1);
            Console.WriteLine();

            Console.WriteLine("Unsubscribe Current Condition Display");
            currentConditionDisplay.UnSubscribe();

            Console.WriteLine("Set Weather the third time");
            weatherPublisher.SetWeatherStats(2, 2, 2);
            Console.WriteLine();

            Console.WriteLine("Unsubscribe Current Statistical Display");
            statisticalDisplay.UnSubscribe();

            Console.WriteLine("Set Weather the fourth time");
            weatherPublisher.SetWeatherStats(3, 3, 3);
            Console.WriteLine();
        }
        public async Task Test_verify_publisher_submit_data_called()
        {
            var data = string.Join(Environment.NewLine, new[]
            {
                "Kochi",
                "30"
            });

            Console.SetIn(new StringReader(data));

            var publisher = new WeatherPublisher(_mockWeatherChannel.Object);

            _mockWeatherChannel.Setup(t => t.ProcessInputDataAsync(It.IsAny <WeatherData>())).Verifiable();
            await publisher.PublishDataAsync();

            _mockWeatherChannel.Verify(t => t.ProcessInputDataAsync(It.IsAny <WeatherData>()), Times.Once);
        }
Esempio n. 3
0
        static async Task Main(string[] args)
        {
            do
            {
                IWeatherChannel  weatherChannel = new WeatherChannel();
                var              subscriber1    = new WeatherSubscriber("Subscriber 1", weatherChannel);
                var              subscriber2    = new WeatherSubscriber("Subscriber 2", weatherChannel);
                WeatherPublisher publisher      = new WeatherPublisher(weatherChannel);

                await publisher.PublishDataAsync();

                subscriber1.UnSubscribe();

                var subscriber3 = new WeatherSubscriber("Subscriber 3", weatherChannel);

                await publisher.PublishDataAsync();

                subscriber2.UnSubscribe();
                subscriber3.UnSubscribe();

                Console.WriteLine("Do you want to continue (Y/N)? ");
            } while (Console.ReadLine().ToUpper() == "Y");
        }