ISubjectdefines registration, removal, and notification methods.IObserverdefinesUpdate;WeatherDataandPersonimplement the corresponding contracts.
9.1 Observer Pattern Lab
This assignment is based off of Head First Design Patterns: p.37-63
Work through the stories in order. Use the acceptance criteria to check each feature, and complete the nested tasks using the specified names, values, and scenarios.
9.1 Observer LastName.zip
9.1 Observer LastName.zipEvery time we want to get new weather data we need to click the button. But the data is constantly being updated in the background causing different people have different knowledge of the weather. Additionally, some of them want a constant update of what the weather is like.
ISubject defines registration, removal, and notification methods.IObserver defines Update; WeatherData and Person implement the corresponding contracts.WeatherData is a class that implements ISubject
<<interface>> ISubject
RegisterObserver(o: IObserver)
RemoveObserver(o: IObserver)
NotifyObservers()
Person is a class that implements IObserver
<<interface>> IObserver
Update()WeatherData stores its observers and supports registration and removal.NotifyObservers invokes Update on the registered observers.List<IObserver> named observers and instantiate it at the field level.RegisterObserver method, call Add on the observers list and pass in the IObserver parameter.RemoveObserver method, call Remove on the observers list and pass in the IObserver parameter.NotifyObservers method, write code that loops through the list of IObservers and calls their Update method.MeasurementsChanged method, call NotifyObservers at the end of the method.Subscribe button. The numbers should update automatically.Subscribe button. Their numbers should also change.Unsubscribe. Their numbers should stop changing.GetWeather method and paste it into the Update method,GetWeatherButton event handler, replace the call to GetWeather with Update.MainWindow.xaml...<Button x:Name="subscribeButton" Content="Subscribe" HorizontalAlignment="Left" Margin="95, 0, 0, 10" VerticalAlignment="Bottom" Width="75"/> <Button x:Name="unsubscribeButton" Content="Unsubscribe" HorizontalAlignment="Left" Margin="180, 0, 0, 10" VerticalAlignment="Bottom" Width="75"/>
SubscribeButton event handler. Get the selected person from the list and pass them in to the call to the…In the SubscribeButton event handler. Get the selected person from the list and pass them in to the call to the weatherData's RegisterObserver method.
UnsubscribeButton event handler. Get the selected person from the list and pass them in to the call to the…In the UnsubscribeButton event handler. Get the selected person from the list and pass them in to the call to the weatherData's RemoveObserver method.
PopulateListBox method with the code below:this.Dispatcher.Invoke(new Action(delegate () {
this.peopleListBox.ItemsSource = null;
this.peopleListBox.ItemsSource = this.people;
}));Subscribe button. The numbers should update automatically.Subscribe button. Their numbers should also change.Unsubscribe. Their numbers should stop changing.HeatAlert implements IObserver and registers with WeatherData.Update behavior matches the specified temperature alert conditions.HeatAlert that implements IObserver.HeatAlert is a class that implements IObserver
HeatAlert
-weatherData: WeatherData
+ HeatAlert(weatherData: WeatherData)
+ Update()
<<interface>> IObserver
Update()Update method, if the weatherData's HeatIndex is equal to 6. If it is, show a MessageBox with a message about it being too hot.MainWindow's constructor, instantiate a HeatAlert and pass it in to the weatherData's RegisterObserver method.MessageBox should appear about it being too hot.This shows how loose coupling works. The Subject doesn't care if the Observer is a Person or a HeatAlert. All it knows is that it's an Observer that has an Update method.
Return to each story and verify its acceptance criteria. Preserve the submission format and destination stated in the activity; a reading, setup guide, lab, video demonstration, and oral final may require different evidence.
Define and implement interfaces based on the class diagram below. Leave the method bodies blank for now. Set up the Subject (WeatherData class) Set up the Observer (Person class) Add an additional observer.