Delegate 的应用

      Delegate是.NET的一个重要特性,并且非常有用。
      也许知道delegate是怎么回事情但是如果没有用过它,就不会更深刻的认识它。好比看的懂某个知识点的代码,但是并不一定真的懂这个知识点。
      对于此,我身有体会,我对delegate的了解就是这样一种情况。
      还记得以前用C/C++写代码,CallBack感觉用起来不顺手。在.net里,delegate可以实现类似的功能。
      举个大家可能碰到过的例子--进度(条)的问题。
      在程序处理一个比较长的过程时,如果能显示进度,对于客户来说是非常棒的事情,但是一个比较大的程序很有可能会分层,而通常会把逻辑放到BLL层。
     这个时候就有一个问题,如何把程序的进度显示给客户呢?答案就是用delegate。
    具体说说吧。
    假设你想合并两个文件。
    步骤是:
     Step1: 读取文件1
     Step 2: 读取文件2
     Step 3:合并文件1 和文件2
    
    如何把这些信息提示给用户呢?
   具体实现如下(伪代码)
  
   A) BLL层

 1 None.gif public   class  Combinator
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif  public string FilePath1;
 4InBlock.gif  public string FilePath2;
 5InBlock.gif  public SetProgressText OnProgressText;
 6InBlock.gif  
 7InBlock.gif  public void Process()
 8ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
 9InBlock.gif    this.OnProgressText(" Loading" +FilePath1+"dot.gif")
10InBlock.gif    Load(FilePath1);
11InBlock.gif        this.OnProgressText(" Loading" +FilePath2+"dot.gif")
12InBlock.gif    Load(FilePath2);
13InBlock.gif       this.OnProgressText("Combining these 2 filesdot.gif");
14InBlock.gif    Combine();
15InBlock.gif   this.OnProgressText("Finished combining these 2 files!");
16ExpandedSubBlockEnd.gif  }

17InBlock.gif
18InBlock.gif  private void Load(string path)
19ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
20InBlock.gif     //The logic to load file
21ExpandedSubBlockEnd.gif   }

22InBlock.gif   private void Combine()
23ExpandedSubBlockStart.gifContractedSubBlock.gifdot.gif{
24InBlock.gif  //The logic to combine 2 files
25ExpandedSubBlockEnd.gif}

26ExpandedSubBlockStart.gifContractedSubBlock.gif  /**////
27InBlock.gif  ///Delegate.
28InBlock.gif  ///To set the progress text to user.
29ExpandedSubBlockEnd.gif  ///

30InBlock.gif  public delegate void SetProgressText(string text);
31ExpandedBlockEnd.gif}
 

  看看Line 5, 30,这里是delegate的定义。

  B) UI层
 
 1 None.gif public  class Form1
 2 ExpandedBlockStart.gifContractedBlock.gif dot.gif {
 3InBlock.gif  private Label lblStatus=new Label();
 4InBlock.gif  private Button btnCombine=new Button();
 5InBlock.gif  private void btnCombine_click(Object o,EventArgs e)
 6ExpandedSubBlockStart.gifContractedSubBlock.gif dot.gif{
 7InBlock.gif  Combinator combine=new Combinator();
 8InBlock.gif  combine.OnSetProgressText=new SetProressText(SetStatus);
 9InBlock.gif  combine.Process();
10ExpandedSubBlockEnd.gif  }

11InBlock.gif  private void SetStatus(string text)
12ExpandedSubBlockStart.gifContractedSubBlock.gif  dot.gif{
13InBlock.gif   this.lblStatus.Text=text;
14ExpandedSubBlockEnd.gif  }

15ExpandedBlockEnd.gif}
 
    看看以上代码的Line8,这里需要给Combinator的OnSetProgressText Delegate一下,指向SetStatus就可以了。注意,SetStatus的参数定义必须和SetProgress的定义相同,不然就不叫delegate,当然编译也通不过。
 
   这是delegate的一个简单应用,但是很有帮助哟。 

  更新:
   Delegate可以传递的,具体如下: 假设A类调用B类,调用了C,而C类又定义了Delegate。可以在B类里定义C类的Delegate属性,然后在A类定义B类的delegate的目标函数,即实现方法。其实A类的实现函数就是C的delegate的目标函数。
                                                                                                                                                            Last Updated 2006年3月23日
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值