- ISubject defines registration, removal, and notification methods.
- IObserver defines Update; WeatherData and Person implement 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.
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()List<IObserver> named observers and instantiate it at the field level.<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"/>
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.
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.
this.Dispatcher.Invoke(new Action(delegate () {
this.peopleListBox.ItemsSource = null;
this.peopleListBox.ItemsSource = this.people;
}));HeatAlert is a class that implements IObserver
HeatAlert
-weatherDataHeatAlert
-weatherData: : WeatherData
+ HeatAlert(weatherData: WeatherData)
+ Update()
<<interface>> IObserver
Update()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.