using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CSharp
{
class Program
{
public interface Subject
{
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}
public interface Observer
{
void update(double temp, double humidity);
}
public interface DisplayElement
{
void Display();
}
public class WeatherData : Subject
{
private List
observers = new List
();
private double temperature, humidity;
public void registerObserver(Observer o)
{
observers.Add(o);
}
public void removeObserver(Observer o)
{
int i = observers.IndexOf(o);
if(i > 0)
observers.Remove(o);
}
public void notifyObservers()
{
foreach(var observer in observers)
observer.update(temperature, humidity);
}
public void setMeasurement(double _temperature, double _humidity)
{
temperature = _temperature;
humidity = _humidity;
notifyObservers();
}
}
public class CurrentConditionsDisplay : Observer, DisplayElement
{
private double temperature, humidity;
private Subject weatherData;
public CurrentConditionsDisplay(Subject _weatherData)
{
weatherData = _weatherData;
weatherData.registerObserver(this);
}
public void update(double _temperature, double _humidity)
{
temperature = _temperature;
humidity = _humidity;
Display();
}
public void Display()
{
Console.WriteLine("Current consditions: {0}°C degrees and {1}% humidity",
temperature, humidity);
}
}
public static void Test()
{
WeatherData weatherData = new WeatherData();
CurrentConditionsDisplay currentConsditionDisplay =
new CurrentConditionsDisplay(weatherData);
weatherData.setMeasurement(12, 32.3);
weatherData.setMeasurement(40, 21);
}
static void Main(string[] args)
{
Test();
}
}
}