Исправление ошибок. Шаблон-наблюдатель - C#
Формулировка задачи:
реализация шаблона для погоды
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace lb2
- {
- class Program
- {
- public static void Main()
- {
- WeatherData weatherData = new WeatherData();
- CarrentConditionDisplay carrentDisplay = new CarrentConditionDisplay(weatherData);
- //...
- weatherData.setMeasurements(80, 65, 30.4);
- weatherData.setMeasurements(82, 70, 29.2);
- weatherData.setMeasurements(78, 90, 29.2);
- Console.Read();
- }
- }
- public interface Subject
- {
- public void registerObserver(Observer 0);
- public void removeObserver(Observer 0);
- public void notifyObservers();
- }
- public interface Observer
- {
- public void update(float temp, float hum, float press);
- }
- public interface DisplayElement
- {
- public void display();
- }
- public class WeatherData : Subject
- {
- // private string SubjectState { get; set; }
- private ArrayList observers;
- private float temp;
- private float hum;
- private float pressure;
- public WeatherData()
- {
- observers = new ArrayList ();
- }
- public void registerObserver(Observer 0)
- {
- observers.Add(0);
- }
- public void removeObserver(Observer 0)
- {
- int i = observers.indexOF(0);
- if (i>=0) observers.remove(i);
- }
- public void notifyObservers()
- {
- foreach (Observer obs in Observers)
- Obs.opdate(temp, hum, pressure);
- }
- public void measurementsChanges()
- {
- notifyObservers();
- }
- public void SetMeasurements(float temp, float hum, float press);
- {
- this.temp = temp;
- this.hum = hum;
- this.pressure = press;
- measurementsChanges();
- }
- //реализовать другие методы класса
- public class CarrentConditionDisplay:Observer, DisplayElement
- {
- private float temper;
- private float humid;
- private Subject weatherData;
- public CarrentConditionDisplay(Subject wData)
- {
- this.weatherData = wData;
- wData.registerObserver(this);
- }
- public void update(float temp, float hum, float press)
- {
- this.temper = temp;
- this.humid = hum;
- Display();
- }
- public void Display()
- {
- System.Console.WriteLine("Текущие условия" + temper + "С и " + humid + "% влажности");
- }
- }
- }
Решение задачи: «Исправление ошибок. Шаблон-наблюдатель»
textual
Листинг программы
- class Program
- {
- static void Main(string[] args)
- {
- var weatherData = new WeatherData();
- CarrentConditionDisplay carrentDisplay = new CarrentConditionDisplay(weatherData);
- weatherData.SetMeasurements(80f, 65f, 30f);
- weatherData.SetMeasurements(82f, 70f, 29f);
- weatherData.SetMeasurements(78f, 90f, 29f);
- Console.Read();
- }
- }
- public interface ISubject
- {
- void RegisterObserver(IObserver observer);
- void RemoveObserver(IObserver observer);
- void NotifyObservers();
- }
- public interface IObserver
- {
- void Update(float temp, float hum, float press);
- }
- public interface IDisplayElement
- {
- void Display();
- }
- public class WeatherData : ISubject
- {
- private readonly IList<IObserver> _observers = new List<IObserver>();
- private float _temp;
- private float _hum;
- private float _pressure;
- public void RegisterObserver(IObserver observer)
- {
- _observers.Add(observer);
- }
- public void RemoveObserver(IObserver observer)
- {
- int i = _observers.IndexOf(observer);
- if (i>=0) _observers.Remove(observer);
- }
- public void NotifyObservers()
- {
- foreach (var observer in _observers)
- observer.Update(_temp, _hum, _pressure);
- }
- public void MeasurementsChanges()
- {
- NotifyObservers();
- }
- public void SetMeasurements(float temp, float hum, float press)
- {
- _temp = temp;
- _hum = hum;
- _pressure = press;
- MeasurementsChanges();
- }
- //реализовать другие методы класса
- }
- public class CarrentConditionDisplay : IObserver, IDisplayElement
- {
- private float _temper;
- private float _humid;
- private ISubject _weatherData;
- public CarrentConditionDisplay(ISubject wData)
- {
- _weatherData = wData;
- wData.RegisterObserver(this);
- }
- public void Update(float temp, float hum, float press)
- {
- _temper = temp;
- _humid = hum;
- Display();
- }
- public void Display()
- {
- System.Console.WriteLine("Текущие условия: " + _temper + "С и " + _humid + "% влажности");
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д