Friday, October 19, 2007

Observer Pattern (2) Subject


// "Subject"

abstract class Stock

{

protected string symbol;

protected double price;

private ArrayList investors = new ArrayList();

// Constructor

public Stock(string symbol, double price)

{

this.symbol = symbol;

this.price = price;

}

public void Attach(Investor investor)

{

investors.Add(investor);

}

public void Detach(Investor investor)

{

investors.Remove(investor);

}

public void Notify()

{

foreach (Investor investor in investors)

{

investor.Update(this);

}

Console.WriteLine("");

}

// Properties

public double Price

{

get { return price; }

set

{

price = value;

Notify();

}

}

public string Symbol

{

get { return symbol; }

set { symbol = value; }

}

}

// "ConcreteSubject"

class IBM : Stock

{

// Constructor

public IBM(string symbol, double price)

: base(symbol, price)

{

}

}

blog comments powered by Disqus