Observer
| 名稱 | Observer |
| 結構 | |
| 意圖 | 定義對象間的一種一對多的依賴關系,當一個對象的狀態發生改變時, 所有依賴于它的對象都得到通知并被自動更新。 |
| 適用性 |
|
| | |
namespace Observer
{
public abstract class Stock // Subject
{
protected string name;
protected double price;
private ArrayList inventors = new ArrayList();
public Stock(string name, double price)
{
this.name = name;
this.price = price;
}
public void Attach(Investor investor)
{
inventors.Add(investor);
}
public void Detach(Investor investor)
{
inventors.Remove(investor);
}
public void Notify()
{
foreach (Investor investor in inventors)
{
investor.Update(this);
}
}
// Properties
public double Price
{
get { return price; }
set
{
price = value;
Notify();
}
}
public Investor Investor
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
}
public class ADSK : Stock
{
public ADSK(string name, double price)
:base(name, price)
{
}
}
public class ABB : Stock
{
public ABB(string name, double price)
:base(name, price)
{
}
}
}
{
public abstract class Stock // Subject
{
protected string name;
protected double price;
private ArrayList inventors = new ArrayList();
public Stock(string name, double price)
{
this.name = name;
this.price = price;
}
public void Attach(Investor investor)
{
inventors.Add(investor);
}
public void Detach(Investor investor)
{
inventors.Remove(investor);
}
public void Notify()
{
foreach (Investor investor in inventors)
{
investor.Update(this);
}
}
// Properties
public double Price
{
get { return price; }
set
{
price = value;
Notify();
}
}
public Investor Investor
{
get
{
throw new System.NotImplementedException();
}
set
{
}
}
}
public class ADSK : Stock
{
public ADSK(string name, double price)
:base(name, price)
{
}
}
public class ABB : Stock
{
public ABB(string name, double price)
:base(name, price)
{
}
}
}
namespace Observer
{
public abstract class Investor // Observer
{
abstract public void Update(Stock stock);
}
public class SmallInvestor : Investor
{
private string name;
public SmallInvestor(string name)
{
this.name = name;
}
override public void Update(Stock stock)
{
Console.WriteLine("Small Investor is Notified! ");
}
}
public class BigInvestor : Investor
{
private string name;
public BigInvestor(string name)
{
this.name = name;
}
override public void Update(Stock stock)
{
Console.WriteLine("Big Investor is Notified! ");
}
}
}
{
public abstract class Investor // Observer
{
abstract public void Update(Stock stock);
}
public class SmallInvestor : Investor
{
private string name;
public SmallInvestor(string name)
{
this.name = name;
}
override public void Update(Stock stock)
{
Console.WriteLine("Small Investor is Notified! ");
}
}
public class BigInvestor : Investor
{
private string name;
public BigInvestor(string name)
{
this.name = name;
}
override public void Update(Stock stock)
{
Console.WriteLine("Big Investor is Notified! ");
}
}
}
namespace Observer
{
class Program
{
static void Main(string[] args)
{
// Create investors/ Observers
SmallInvestor s = new SmallInvestor("Small Investor");
BigInvestor b = new BigInvestor("Big Investor");
ADSK adsk = new ADSK("ADSK", 46.0);
ABB abb = new ABB("ABB", 23.4);
adsk.Attach(s);
adsk.Attach(b);
abb.Attach(s);
abb.Attach(b);
adsk.Price = 48;
abb.Price = 26;
return;
}
}
}
{
class Program
{
static void Main(string[] args)
{
// Create investors/ Observers
SmallInvestor s = new SmallInvestor("Small Investor");
BigInvestor b = new BigInvestor("Big Investor");
ADSK adsk = new ADSK("ADSK", 46.0);
ABB abb = new ABB("ABB", 23.4);
adsk.Attach(s);
adsk.Attach(b);
abb.Attach(s);
abb.Attach(b);
adsk.Price = 48;
abb.Price = 26;
return;
}
}
}
posted on 2011-06-11 22:32 Mike Song 閱讀(281) 評論(0) 編輯 收藏 引用 所屬分類: Design Pattern

