柚子Nan--回归原点

Everything can be as easy as you like or as complex as you need.
  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

用Observer实现的Stock代码

Posted on 2004-12-03 15:21  柚子Nan  阅读(403)  评论(0)    收藏  举报
Stock
public delegate void PriceChangeDelegate(Stock aStock);
    
public class Stock
    
{
        
public event PriceChangeDelegate PriceChangeHandler;
        
protected string symbol;
        
protected double price;

        
public Stock( string symbol, double price )
        
{
            
this.symbol = symbol;
            
this.price = price;
        }

        
public void Notify()
        
{
            
if(PriceChangeHandler != null)
                PriceChangeHandler(
this);
        }

        
// Properties
        public double Price
        
{
            
getreturn price; }
            
set{ price = value;}
        }


        
public string Symbol
        
{
            
getreturn symbol; }
            
set{ symbol = value; }
        }

    }


Inversotr
interface IInvestor
    
{
        
void Update(Stock aStock);
    }


    
public class  Inverstor : IInvestor
    
{
        
private string name;

        
// Constructors
        public Inverstor( string name )
        
{
            
this.name = name;
        }


        
IInvestor 成员

    }


前台程序
            ObserverAdvance.Inverstor Koffer = new ObserverAdvance.Inverstor("My WebSite");
            ObserverAdvance.Stock HuaxiaStock 
= new ObserverAdvance.Stock("HuaXiaIT",100);
            HuaxiaStock.PriceChangeHandler 
+= new ObserverAdvance.PriceChangeDelegate(Koffer.Update);
            
            HuaxiaStock.Price 
= 200;
            HuaxiaStock.Notify();