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,25 @@
using System;
namespace ObserverPattern.StockUpdateEvents
{
public class LondonStockObserver
{
double? oldValue;
public LondonStockObserver(StockSubject stockObservable)
{
stockObservable.StockUpdated += (obj, e) => PrintStockValue(e.Stock);
}
private void PrintStockValue(Stock stock)
{
if (stock.Name == "FTSE")
{
if (oldValue.HasValue)
Console.WriteLine("The Financial Times Stock Exchange 100 Index price updated from {0} to {1}", oldValue, stock.Value);
else
Console.WriteLine("The Financial Times Stock Exchange 100 Index has a new price, new value is: {0}", stock.Value);
oldValue = stock.Value;
}
}
}
}