2021-09-14 12:31:36 +12:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2021-09-21 20:07:08 +12:00
|
|
|
|
using System.ComponentModel;
|
|
|
|
|
using System.Data;
|
|
|
|
|
using System.Drawing;
|
2021-09-14 12:31:36 +12:00
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
2021-09-21 20:07:08 +12:00
|
|
|
|
using System.Windows.Forms;
|
2021-09-14 12:31:36 +12:00
|
|
|
|
|
|
|
|
|
namespace Tutorial_5
|
|
|
|
|
{
|
2021-09-21 20:07:08 +12:00
|
|
|
|
public partial class WeatherData : Form, ISubject
|
2021-09-14 12:31:36 +12:00
|
|
|
|
{
|
|
|
|
|
List<IWeatherObserver> observers;
|
|
|
|
|
public void DeregisterObserver(IWeatherObserver observer)
|
|
|
|
|
{
|
|
|
|
|
if (observers.Contains(observer)) observers.Remove(observer);
|
|
|
|
|
else Console.Error.WriteLine("{0} does not exist in the observers list.", observer.ToString());
|
|
|
|
|
}
|
|
|
|
|
|
2021-09-21 20:07:08 +12:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// 'Push' method
|
|
|
|
|
/// </summary>
|
2021-09-14 12:31:36 +12:00
|
|
|
|
public void NotifyObservers()
|
|
|
|
|
{
|
|
|
|
|
double t = GetTemperature();
|
|
|
|
|
double h = GetHumidity();
|
|
|
|
|
double p = GetPressure();
|
|
|
|
|
|
|
|
|
|
foreach (var observer in observers)
|
|
|
|
|
{
|
2021-09-21 20:07:08 +12:00
|
|
|
|
observer.Update(t, h, p);
|
2021-09-14 12:31:36 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void RegisterObserver(IWeatherObserver observer)
|
|
|
|
|
{
|
|
|
|
|
if (observers.Contains(observer)) Console.Error.WriteLine("{0} already exists in the observers list.", observer.ToString());
|
|
|
|
|
else observers.Add(observer);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double GetTemperature()
|
|
|
|
|
{
|
2021-09-21 20:07:08 +12:00
|
|
|
|
return TemperatureSlider.Value;
|
2021-09-14 12:31:36 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double GetHumidity()
|
|
|
|
|
{
|
2021-09-21 20:07:08 +12:00
|
|
|
|
return HumiditySlider.Value;
|
2021-09-14 12:31:36 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public double GetPressure()
|
|
|
|
|
{
|
2021-09-21 20:07:08 +12:00
|
|
|
|
return PressureSlider.Value;
|
2021-09-14 12:31:36 +12:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void MeasurementChanged()
|
|
|
|
|
{
|
|
|
|
|
NotifyObservers();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public WeatherData()
|
|
|
|
|
{
|
2021-09-21 20:07:08 +12:00
|
|
|
|
InitializeComponent();
|
2021-09-14 12:31:36 +12:00
|
|
|
|
observers = new List<IWeatherObserver>();
|
|
|
|
|
}
|
2021-09-21 20:07:08 +12:00
|
|
|
|
|
|
|
|
|
private void Form1_Load(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Analagous to the Update() method in Excercise 1
|
|
|
|
|
/// Pushes data to observers via Subject.NotifyObservers
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
private void SliderChanged(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
NotifyObservers();
|
|
|
|
|
TemperatureLabel.Text = TemperatureSlider.Value.ToString() + " ℃";
|
|
|
|
|
HumidityLabel.Text = HumiditySlider.Value.ToString() + " %";
|
|
|
|
|
PressureLabel.Text = PressureSlider.Value.ToString() + " kPa";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void Form1_Load_1(object sender, EventArgs e)
|
|
|
|
|
{
|
|
|
|
|
CurrentConditionsDisplay ccd = new CurrentConditionsDisplay(this);
|
|
|
|
|
ccd.Show();
|
|
|
|
|
}
|
2021-09-14 12:31:36 +12:00
|
|
|
|
}
|
|
|
|
|
}
|