C# 通知机制 IObserver<T> 和 IObservable<T>
2021-06-22 19:03
标签:lis ted contains report struct can span display The C# 通知机制 IObserver 标签:lis ted contains report struct can span display The 原文地址:https://www.cnblogs.com/kikyoqiang/p/10208783.html class Program
{
public static void Main()
{
// Define a provider and two observers.
LocationTracker provider = new LocationTracker();
LocationReporter reporter1 = new LocationReporter("FixedGPS");
reporter1.Subscribe(provider);
LocationReporter reporter2 = new LocationReporter("MobileGPS");
reporter2.Subscribe(provider);
provider.TrackLocation(new Location(47.6456, -122.1312));
reporter1.Unsubscribe();
provider.TrackLocation(new Location(47.6677, -122.1199));
provider.TrackLocation(null);
provider.EndTransmission();
Console.ReadLine();
// The example displays output similar to the following:
// FixedGPS: The current location is 47.6456, -122.1312
// MobileGPS: The current location is 47.6456, -122.1312
// MobileGPS: The current location is 47.6677, -122.1199
// MobileGPS: The location cannot be determined.
// The Location Tracker has completed transmitting data to MobileGPS.
}
}
public struct Location
{
double lat, lon;
public Location(double latitude, double longitude)
{
this.lat = latitude;
this.lon = longitude;
}
public double Latitude
{ get { return this.lat; } }
public double Longitude
{ get { return this.lon; } }
}
public class LocationReporter : IObserver
文章标题:C# 通知机制 IObserver<T> 和 IObservable<T>
文章链接:http://soscw.com/index.php/essay/97494.html