static void Main(string[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData); Measurements measurements = new Measurements { humidity = 65F, pressure = 30.4F, temperature = 80F }; weatherData.SetMeasurements(measurements); measurements.humidity = 70F; measurements.pressure = 29.2F; measurements.temperature = 82F; weatherData.SetMeasurements(measurements); measurements.humidity = 90F; measurements.pressure = 29.2F; measurements.temperature = 78F; weatherData.SetMeasurements(measurements); }
public static void Main(string[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentConditions = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); Console.ReadLine(); }
static void Main(string[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay ccd = new CurrentConditionsDisplay(weatherData); ForecastDisplay fd = new ForecastDisplay(weatherData); StatisticsDisplay sd = new StatisticsDisplay(weatherData); ThirdPartyDisplay tpd = new ThirdPartyDisplay(weatherData); weatherData.SetMeasurements(15, 75, 30); weatherData.SetMeasurements(18, 80, 31.2); weatherData.SetMeasurements(18, 75, 32.8); Console.ReadKey(true); }
static void Main(string[] args) { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); weatherData.setMeasurements(82, 70, 29.2f); weatherData.setMeasurements(78, 90, 29.2f); }
public void Main() { WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentConditionsDisplay = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData); weatherData.SetMeasurementsChanged(80, 65, 30.4f); Console.WriteLine("==========================================="); weatherData.SetMeasurementsChanged(82, 70, 29.2f); Console.WriteLine("==========================================="); weatherData.SetMeasurementsChanged(78, 90, 29.2f); }
static void Main(string[] args) { WeatherData weatherData = new WeatherData(); //Инициализация наблюдателей CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); //Обновление данных weatherData.setMeasurements(80, 45, 34.5f); //Удаление одного из наблюдателей statisticsDisplay.remove(); weatherData.setMeasurements(45, 34, 37.6f); weatherData.setMeasurements(49, 45, 38.5f); }
static void Main(string[] args) { WeatherData weatherData = new WeatherData(); Console.WriteLine("--------公告板1加入观察者-------"); CurrentConditionDisplay display1 = new CurrentConditionDisplay(weatherData); weatherData.SetMessureMents(10, 20, 30); Console.WriteLine("--------公告板2加入观察者-------"); ForecastDisplay display2 = new ForecastDisplay(weatherData); weatherData.SetMessureMents(15, 25, 35); Console.WriteLine("--------公告板1退出观察者-------"); weatherData.RemoveObserver(display1); weatherData.SetMessureMents(19, 29, 39); }
static void Main(string[] args) { var meteostation = new Meteostation(); var weatherData = new WeatherData(meteostation); var currentConditionDisplay = new CurrentConditionDisplay(weatherData); meteostation.mesurementsChanged(); var forecastDisplay = new ForecastDisplay(weatherData); meteostation.mesurementsChanged(); currentConditionDisplay.Unsubscribe(); meteostation.mesurementsChanged(); var statisticsDisplay = new StatisticsDisplay(weatherData); currentConditionDisplay.Subscribe(weatherData); meteostation.mesurementsChanged(); }
static void Main() { // First example ConcreteSubject s = new ConcreteSubject(); s.Attach(new ConcreteObserver(s, "x")); s.Attach(new ConcreteObserver(s, "y")); s.Attach(new ConcreteObserver(s, "z")); s.SubjectState = "abc"; s.Notify(); IBM ibm = new IBM("IBM", 120.00); ibm.Attach(new Investor("Sorros")); ibm.Attach(new Investor("Berkshire")); ibm.Price = 120.10; ibm.Price = 121.00; ibm.Price = 120.50; ibm.Price = 120.75; ibm.Notify(); // Second example BaggageHandler provider = new BaggageHandler(); ArrivalsMonitor observer1 = new ArrivalsMonitor("BaggageClaimMonitor1"); ArrivalsMonitor observer2 = new ArrivalsMonitor("SecurityExit"); provider.BaggageStatus(712, "Detroit", 3); observer1.Subscribe(provider); provider.BaggageStatus(712, "Kalamazoo", 3); provider.BaggageStatus(400, "New York-Kennedy", 1); provider.BaggageStatus(712, "Detroit", 3); observer2.Subscribe(provider); provider.BaggageStatus(511, "San Francisco", 2); provider.BaggageStatus(712); observer2.Unsubscribe(); provider.BaggageStatus(400); provider.LastBaggageClaimed(); // Third example var weatherData = new WeatherData(); var forecastDisplay = new ForecastDisplay(weatherData); var heatIndexDisplay = new HeatIndexDisplay(weatherData); weatherData.setMeasurements(80, 65, 30.4f); }
static void Main(string[] args) { Console.WriteLine("Weather Station"); Console.WriteLine(); WeatherData weatherData = new WeatherData(); CurrentConditionsDisplay currentDisplay = new CurrentConditionsDisplay(weatherData); StatisticsDisplay statisticsDisplay = new StatisticsDisplay(weatherData); ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); HeatIndexDisplay heatIndexDisplay = new HeatIndexDisplay(weatherData); weatherData.SetMeasurements(80, 65, 30.4f); Console.WriteLine(); weatherData.SetMeasurements(87, 70, 29.2f); Console.WriteLine(); weatherData.SetMeasurements(78, 90, 29.2f); Console.WriteLine(); weatherData.SetMeasurements(65, 75, 29.2f); Console.WriteLine(); int temp, hum; float press; Console.Write("What is the current Temperature? T: "); temp = Int32.Parse(Console.ReadLine()); Console.Write("What is the current Humidity? H: "); hum = Int32.Parse(Console.ReadLine()); Console.Write("What is the current Pressure? P: "); press = float.Parse(Console.ReadLine()); Console.WriteLine(); weatherData.SetMeasurements(temp, hum, press); Console.WriteLine(); Console.ReadKey(); }
static void Main(string[] args) { /* * The Observer Pattern * * Example: * A publisher creates a new magazine and begins publishing issues. * You subscribve and receive issues as along as you stay subscribed. * You can unsubscribe at any time. * Others can also subscribe. * Other can also unsubscribe. * If Publisher ceases business, you stop receiving issues. * * Allows: * -Subjests and Observers are "loosely coupled" * -Strive for loosely coupled designs between objects that interact. (Design Principle #4) * * Implementation (Push): * * We define 3 interfaces: * -ISubject (implemented by publisher, stores the list of subscribers, notifies on updates) * -IObserver (implemented by subscriber, update method) * -IDisplayElement (implemented by subscriber, display method) * * We define main class WatherData: * -WatherData that implements ISubject * -Notifies observers(subscribers) though method Update() * -This doesn't know anything about observers * -Subject doesn't care it keeps doing its job * * We define subscriber classes: * -Each implements interfaces IObserver and IDisplayElement just to display info * -Each receives Subject(Publisher) in constructor, subscribes itself to it, and keeps it in private field * -To become an observer it needs to implement IObserver * */ //create concrete subject(publisher) WeatherData weatherData = new WeatherData(); //create Observer(subscriber) and register with Subject(publisher) CurrentConditionsDisplay currentConditions = new CurrentConditionsDisplay(weatherData); //subscribe in constructor of observer(subscriber), PUSH subscription //create more Observers(subscribers) weatherData.RegisterObserver(new StatisticsDisplay(weatherData)); //subscribe in method of subject(publisher), PULL subscription ForecastDisplay forecastDisplay = new ForecastDisplay(weatherData); //refresh data at Subject(publisher), simulate new data from sensors weatherData.SetMeasurements(new Data() { Temp = 80, Humidity = 65, Pressure = 29.2 }); weatherData.SetMeasurements(new Data() { Temp = 82, Humidity = 70, Pressure = 30.4 }); weatherData.SetMeasurements(new Data() { Temp = 78, Humidity = 90, Pressure = 29.4 }); Console.ReadLine(); }