Defines a provider for push-based notification.
Assembly: mscorlib (in mscorlib.dll)
- out T
The object that provides notification information.
This type parameter is covariant. That is, you can use either the type you specified or any type that is more derived. For more information about covariance and contravariance, see Covariance and Contravariance in Generics.
Thetype exposes the following members.
The IObserver<T> and interfaces provide a generalized mechanism for push-based notification, also known as the observer design pattern. The interface represents the class that sends notifications (the provider); the IObserver<T> interface represents the class that receives them (the observer). T represents the class that provides the notification information. In some push-based notifications, the IObserver<T> implementation and T can represent the same type.
The provider must implement a single method, Subscribe, that indicates that an observer wants to receive push-based notifications. Callers to the method pass an instance of the observer. The method returns an IDisposable implementation that enables observers to cancel notifications at any time before the provider has stopped sending them.
At any given time, a given provider may have zero, one, or multiple observers. The provider is responsible for storing references to observers and ensuring that they are valid before it sends notifications. The interface does not make any assumptions about the number of observers or the order in which notifications are sent.
The provider sends the following three kinds of notifications to the observer by calling IObserver<T> methods:
The current data. The provider can call the IObserver<T>::OnNext method to pass the observer a T object that has current data, changed data, or fresh data.
An error condition. The provider can call the IObserver<T>::OnError method to notify the observer that some error condition has occurred.
No further data. The provider can call the IObserver<T>::OnCompleted method to notify the observer that it has finished sending notifications.
The following example illustrates the observer design pattern. It defines a Location class that contains latitude and longitude information.
The LocationTracker class provides the implementation. Its TrackLocation method is passed a nullable Location object that contains the latitude and longitude data. If the Location value is not nullptr, the TrackLocation method calls the OnNext method of each observer.
If the Location value is nullptr, the TrackLocation method instantiates a LocationNotFoundException object, which is shown in the following example. It then calls each observer's OnError method and passes it the LocationNotFoundException object. Note that LocationNotFoundException derives from Exception, but does not add any new members.
Observers register to receive notifications from a TrackLocation object by calling its IObservable<T>::Subscribe method, which assigns a reference to the observer object to a private generic List<T> object. The method returns an Unsubscriber object, which is an IDisposable implementation that enables observers to stop receiving notifications. The LocationTracker class also includes an EndTransmission method. When no further location data is available, the method calls each observer's OnCompleted method and then clears the internal list of observers.
In this example, the LocationReporter class provides the IObserver<T> implementation. It displays information about the current location to the console. Its constructor includes a name parameter, which enables the LocationReporter instance to identify itself in its string output. It also includes a Subscribe method, which wraps a call to the provider's Subscribe method. This allows the method to assign the returned IDisposable reference to a private variable. The LocationReporter class also includes an Unsubscribe method, which calls the IDisposable::Dispose method of the object that is returned by the IObservable<T>::Subscribe method. The following code defines the LocationReporter class.
The following code then instantiates the provider and the observer.
Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)