Implement Observer Design Pattern in C# - Real World Example Brokers watching company Share Price
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ShareManager
{
abstract class Shares
{
private string _companysymbol;
private double _shareprice;
private List<ShareHolder> _shareholders = new List<ShareHolder>();
public Shares(string companysymbol, double shareprice)
{
this._companysymbol = companysymbol;
this._shareprice = shareprice;
}
public void Watch(ShareHolder shareholder)
{
_shareholders.Add(shareholder);
}
public void Detach(ShareHolder shareholder)
{
_shareholders.Remove(shareholder);
}
public void Notify()
{
foreach (ShareHolder shareholder in _shareholders)
{
shareholder.Update(this);
}
Console.WriteLine("");
}
public double SharePrice
{
get { return _shareprice; }
set
{
if (_shareprice != value)
{
_shareprice = value;
Notify();
}
}
}
// Gets the companysymbol
public string CompanySymbol
{
get { return _companysymbol; }
}
}
class CompanyShare : Shares
{
public CompanyShare(string companysymbol, double shareprice)
: base(companysymbol, shareprice)
{
}
}
interface ShareHolder
{
void Update(Shares companysharevalue);
}
class Broker : ShareHolder
{
private string _name;
public Broker(string name)
{
this._name = name;
}
public void Update(Shares companysharevalue)
{
Console.WriteLine("Notified broker {0} of {1}'s share price" +
" change to £{2:F}", _name, companysharevalue.CompanySymbol, companysharevalue.SharePrice);
}
}
class ShareManager
{
static void Main()
{
CompanyShare _kg = new CompanyShare("Kerry Group", 26.00);
_kg.Watch(new Broker("Davys"));
_kg.Watch(new Broker("Goodbodys"));
_kg.Watch(new Broker("NCB"));
CompanyShare _aib = new CompanyShare("AIB", 0.55);
_aib.Watch(new Broker("Davys"));
_aib.Watch(new Broker("Goodbodys"));
_aib.Watch(new Broker("NCB"));
_kg.SharePrice = 26.10;
_kg.SharePrice = 26.30;
_aib.SharePrice = 00.52;
_kg.SharePrice = 26.75;
Console.ReadKey();
}
}
}
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
namespace ShareManager
{
abstract class Shares
{
private string _companysymbol;
private double _shareprice;
private List<ShareHolder> _shareholders = new List<ShareHolder>();
public Shares(string companysymbol, double shareprice)
{
this._companysymbol = companysymbol;
this._shareprice = shareprice;
}
public void Watch(ShareHolder shareholder)
{
_shareholders.Add(shareholder);
}
public void Detach(ShareHolder shareholder)
{
_shareholders.Remove(shareholder);
}
public void Notify()
{
foreach (ShareHolder shareholder in _shareholders)
{
shareholder.Update(this);
}
Console.WriteLine("");
}
public double SharePrice
{
get { return _shareprice; }
set
{
if (_shareprice != value)
{
_shareprice = value;
Notify();
}
}
}
// Gets the companysymbol
public string CompanySymbol
{
get { return _companysymbol; }
}
}
class CompanyShare : Shares
{
public CompanyShare(string companysymbol, double shareprice)
: base(companysymbol, shareprice)
{
}
}
interface ShareHolder
{
void Update(Shares companysharevalue);
}
class Broker : ShareHolder
{
private string _name;
public Broker(string name)
{
this._name = name;
}
public void Update(Shares companysharevalue)
{
Console.WriteLine("Notified broker {0} of {1}'s share price" +
" change to £{2:F}", _name, companysharevalue.CompanySymbol, companysharevalue.SharePrice);
}
}
class ShareManager
{
static void Main()
{
CompanyShare _kg = new CompanyShare("Kerry Group", 26.00);
_kg.Watch(new Broker("Davys"));
_kg.Watch(new Broker("Goodbodys"));
_kg.Watch(new Broker("NCB"));
CompanyShare _aib = new CompanyShare("AIB", 0.55);
_aib.Watch(new Broker("Davys"));
_aib.Watch(new Broker("Goodbodys"));
_aib.Watch(new Broker("NCB"));
_kg.SharePrice = 26.10;
_kg.SharePrice = 26.30;
_aib.SharePrice = 00.52;
_kg.SharePrice = 26.75;
Console.ReadKey();
}
}
}
Jacket Leather Indonesia
ReplyDeleteCivet Coffee Real Indonesia
cd interaktif bantal silikon pembicara marketing anne jelita brenda anne caca tasya ruth
bantal
web
distrobantal.blogspot.com/
bantalsilikon01.blogspot.com/
sabunherbalya.blogspot.com/
kesethandukmalang
bantalmalangmurah
restoran-seo
distro-seo
jualsangkarpleci.blogspot.com/
marinir-seo.blogspot.com/
bumbu-pecel-malang.blogspot.com/
CD,marinir,bantal-silikon15.blogspot.com,bumbu-,seo-guru01,seo,seo,http://dfes12/,agen,seo/,seo16,age,jk,jk,seo,guru,seo,seo,vvt,agen,asdf,banta,laris,bantal,999,