class DataGenerator : IObservable<int>
{
private List<IObserver<int>> _observers = new List<IObserver<int>>();
private int _lastPrime = -1;
//inherited from IObservable<T>
public IDisposable Subscribe(IObserver<int> observer)
{
_observers.Add(observer);
observer.OnNext(_lastPrime);
return observer as IDisposable;
}
//notifies all subscribers of the new data
private void NotifyData(int n)
{
foreach (IObserver<int> observer in _observers)
{
observer.OnNext(n);
}
}
//notifies all subscribers that no more data is coming
private void NotifyComplete()
{
foreach (IObserver<int> observer in _observers)
{
observer.OnCompleted();
}
}
private static Random rand = new Random();
//let's just generate some aribtrary data
public void Run()
{
for (int i=0;i<100;++i)
{
int n = rand.Next(1, Int32.MaxValue);
if (IsPrime(n))
{
_lastPrime = n;
NotifyData(n);
}
}
NotifyComplete();
}
private static bool IsPrime(Int32 number)
{
//check for evenness
if (number % 2 == 0)
{
if (number == 2)
return true;
return false;
}
//don't need to check past the square root
Int32 max = (Int32)Math.Sqrt(number);
for (Int32 i = 3; i <= max; i += 2)
{
if ((number % i) == 0)
{
return false;
}
}
return true;
}
}
class DataObserver : IObserver<int>
{
//give it a name so we can distinguish it in the output
private string _name = "Observer";
#region IObserver<int> Members
public void OnCompleted()
{
Console.WriteLine(_name + ":Completed");
}
public void OnError(Exception error)
{
Console.WriteLine(_name + ": Error");
}
public void OnNext(int value)
{
Console.WriteLine(_name + ":Generated data {0}", value);
}
#endregion
public DataObserver(string observerName)
{
_name = observerName;
}
}
DataGenerator generator = new DataGenerator();
DataObserver observer1 = new DataObserver("O1");
DataObserver observer2 = new DataObserver("O2");
generator.Subscribe(observer1);
generator.Subscribe(observer2);
generator.Run();
O1:Generated data -1
O2:Generated data -1
O1:Generated data 597759749
O2:Generated data 597759749
O1:Generated data 369128117
O2:Generated data 369128117
O1:Generated data 650236453
O2:Generated data 650236453
O1:Generated data 2143508953
O2:Generated data 2143508953
O1:Generated data 298906169
O2:Generated data 298906169
O1:Generated data 1296076711
O2:Generated data 1296076711
O1:Generated data 1970737339
O2:Generated data 1970737339
O1:Completed
O2:Completed
Press any key to exit...