Add stock example on Observer pattern

This commit is contained in:
Petrutiu Mihai
2016-06-28 17:26:33 +03:00
parent 4a69b3da17
commit 5d639bf0ab
13 changed files with 262 additions and 34 deletions

View File

@@ -0,0 +1,29 @@
using System;
namespace ObserverPattern.StockUpdateEvents
{
/// <summary>
/// Observer
/// </summary>
public class AaplObserver
{
double? oldValue;
public AaplObserver(StockSubject stockObservable)
{
stockObservable.StockUpdated += (obj, e) => PrintNewValue(e.Stock);
}
private void PrintNewValue(Stock stock)
{
if (stock.Name == "AAPL")
{
if (oldValue.HasValue)
Console.WriteLine("Apple price updated from {0} to {1}", oldValue, stock.Value);
else
Console.WriteLine("Apple has a new price, new value is: {0}", stock.Value);
oldValue = stock.Value;
}
}
}
}