CSE459_CSharp_04_ObserverPattern

CSE459_CSharp_04_ObserverPattern - Programming in C#...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Programming in C# Observer Design Pattern Observer Design Pattern CSE 494R (proposed course for 459 Programming in C#) Prof. Roger Crawfis
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Keeping Objects Informed A very common requirement in any complex system is the ability for an object to know whenever an event happens or whenever another object changes its state (which can also be viewed as an event). Examples: Interest rates lowered File I/O request is complete Mouse button was depressed.
Background image of page 2
Polling vs. Notification There are two primary approaches: Polling – Any object that is interested in state changes will periodical inquire the state. Difficult to get to work with events. Notification – Objects register to receive an update or notification whenever state changes or an event occurs. A publisher ensures that everyone is notified. Observer Design Pattern
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Observer Pattern This one of the common design patterns published by Gamma, et. al. Also goes by the name Publisher-Subscriber. When one object changes state, all the dependent objects are notified and updated. Allows for consistency between related objects without tightly coupling classes e.g. “reduces coupling between objects”
Background image of page 4
A Problem Multiple displays need to be updated with weather data from a single weather station ©Head First Design Patterns, Freeman and Freeman, p. 39
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
A naive solution public class WeatherData { // instance variable declarations public void MeasurementsChanged() { float temperature = getTemperature(); float humidity = getHumidity(); float pressure = getPressure(); currentConditionsDisplay.update( temperature, humidity, pressure);
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/27/2012 for the course CSE 459.24 taught by Professor Crawfis during the Winter '11 term at Ohio State.

Page1 / 19

CSE459_CSharp_04_ObserverPattern - Programming in C#...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online