开发学院软件开发C语言 适合C# Actor的消息执行方式(6):协变与逆变 阅读

适合C# Actor的消息执行方式(6):协变与逆变

 2010-09-30 20:50:09 来源:WEB开发网 闂傚倸鍊搁崐椋庢濮橆兗缂氱憸宥堢亱闂佸湱铏庨崰鏍不椤栫偞鐓ラ柣鏇炲€圭€氾拷闂傚倸鍊搁崐椋庣矆娓氣偓楠炲鏁撻悩鎻掔€梺姹囧灩閻忔艾鐣烽弻銉︾厵闁规鍠栭。濂告煕鎼达紕校闁靛洤瀚伴獮鎺楀箣濠靛啫浜鹃柣銏⑶圭壕濠氭煙閻愵剚鐏辨俊鎻掔墛缁绘盯宕卞Δ鍐冣剝绻涘畝濠佺敖缂佽鲸鎹囧畷鎺戭潩閹典焦鐎搁梻浣烘嚀閸ゆ牠骞忛敓锟�婵犵數濮烽弫鍛婃叏椤撱垹绠柛鎰靛枛瀹告繃銇勯幘瀵哥畼闁硅娲熷缁樼瑹閳ь剙岣胯鐓ら柕鍫濇偪濞差亜惟闁宠桨鑳堕崝锕€顪冮妶鍡楃瑐闁煎啿鐖奸崺濠囧即閵忥紕鍘梺鎼炲劗閺呮稒绂掕缁辨帗娼忛埡浣锋闂佽桨鐒﹂幑鍥极閹剧粯鏅搁柨鐕傛嫹闂傚倸鍊搁崐椋庢濮橆兗缂氱憸宥堢亱闂佸湱铏庨崰鏍不椤栫偞鐓ラ柣鏇炲€圭€氾拷  闂傚倸鍊搁崐鐑芥嚄閼哥數浠氱紓鍌欒兌缁垶銆冮崨鏉戠厺鐎广儱顦崡鎶芥煏韫囨洖校闁诲寒鍓熷铏圭磼濡搫顫岄梺鍦拡閸嬪棝鎯€椤忓浂妯勯梺鍝勬湰濞叉ḿ鎹㈠┑濠勭杸闁哄洨濮烽悰銉╂⒒娴e搫甯跺鐟帮攻缁傚秴饪伴崼姘e亾閺冨牆绀冩い蹇庣娴滈箖鏌ㄥ┑鍡涱€楀褜鍠栭湁闁绘ɑ鐟ョ€氼喚绮绘ィ鍐╃厱妞ゆ劑鍊曢弸搴ㄦ煟韫囧鍔滈柕鍥у瀵潙螣閸濆嫬袝婵$偑鍊戦崹娲偡閳哄懎绠栭柍鈺佸暞閸庣喖鏌曢崶褍绨婚柟鍑ゆ嫹
核心提示: 为了让Monior支持查询,我们还需要为它添加这样的代码:publicclassMonitor:Actor<Action<Monitor>>,IPort<Monitor>,ICrawlResponseHandler,IStatisticRequestHande

为了让Monior支持查询,我们还需要为它添加这样的代码:

public class Monitor : Actor<Action<Monitor>>, IPort<Monitor>, 
  ICrawlResponseHandler, 
  IStatisticRequestHandelr 
{ 
  ... 
 
  #region IStatisticRequestHandelr Members 
 
  void IStatisticRequestHandelr.GetCrawledCount(IPort<IStatisticResponseHandler> requester) 
  { 
    requester.Post(r => r.ReplyCrawledCount(this.m_urlContent.Count)); 
  } 
 
  void IStatisticRequestHandelr.GetContent(IPort<IStatisticResponseHandler> requester, string url) 
  { 
    string content; 
    if (!this.m_urlContent.TryGetValue(url, out content)) 
    { 
      content = null; 
    } 
 
    requester.Post(r => r.ReplyContent(url, content)); 
  } 
 
  #endregion 
}

最后,我们来尝试着使用这个“查询”功能。首先,我们编写一个测试用的TestStatisticPort类:

public class TestStatisticPort : IPort<IStatisticResponseHandler>, IStatisticResponseHandler 
{ 
  private IPort<IStatisticRequestHandelr> m_statisticPort; 
 
  public TestStatisticPort(IPort<IStatisticRequestHandelr> statisticPort) 
  { 
    this.m_statisticPort = statisticPort; 
  } 
 
  public void Start() 
  { 
    while (true) 
    { 
      Console.ReadLine(); 
      this.m_statisticPort.Post(s => s.GetCrawledCount(this)); 
    } 
  } 
 
  #region IPort<IStatisticResponseHandler> Members 
 
  void IPort<IStatisticResponseHandler>.Post(Action<IStatisticResponseHandler> message) 
  { 
    message(this); 
  } 
 
  #endregion 
 
  #region IStatisticResponseHandler Members 
 
  void IStatisticResponseHandler.ReplyCrawledCount(int count) 
  { 
    Console.WriteLine("Crawled: {0}", count); 
  } 
 
  void IStatisticResponseHandler.ReplyContent(string url, string content) { ... } 
 
  #endregion 
}

上一页  1 2 3 4 5 6 7  下一页

Tags:适合 Actor 消息

编辑录入:爽爽 [复制链接] [打 印]
[]
  • 好
  • 好的评价 如果觉得好,就请您
      0%(0)
  • 差
  • 差的评价 如果觉得差,就请您
      0%(0)
赞助商链接