解讀asp.net中的觀察者模式(3)_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:解讀Asp.net教程:設(shè)計(jì)IP地址屏蔽功能
      出于安全考慮,幾乎每個(gè)動(dòng)態(tài)網(wǎng)站都具備IP地址屏蔽功能,而網(wǎng)上流傳的很多關(guān)于該功能的教程大都采用字符串保存和驗(yàn)證IP地址,我認(rèn)為這是不太科學(xué)的,我試圖找到最佳的設(shè)計(jì)方案。 “I

      經(jīng)過一番思考后我還是回到我自己的Solution上,不過我又有了新的看法了。不是要將數(shù)據(jù)先緩存么?看看這個(gè)緩存,實(shí)際上她也是個(gè)觀察者,至于她執(zhí)行怎樣的緩存策略是她的事情,如是我又有了一個(gè)新類:

      以下為引用的內(nèi)容:

      //這里的代碼接上篇Post
      using System;
      using System.Collections.Generic;
      using System.Text;
      namespace ForyourSoft.NetTraffic.Framework
      {
      public sealed class DataBase
      {
      private IList<string> _buffer = new List<string>();
      private static DataBase _instance = null;
      public static void Subscribe()
      {
      if (_instance == null)
      _instance = new DataBase(Monitor.Current);
      }
      public DataBase(Monitor monitor)
      {
      monitor.OnMessage = new EventHandler<Monitor.MessageEventArgs>(monitor_OnMessage);
      }
      void monitor_OnMessage(object sender, Monitor.MessageEventArgs e)
      {
      _buffer.Add(e.Message);
      if (_buffer.Count >= Config.BufferSize)
      {
      //將數(shù)據(jù)添加到數(shù)據(jù)庫
      }
      }
      }
      }

      PS:由于系統(tǒng)中我們只需要這樣唯一一個(gè)訂閱者,所以我將其實(shí)現(xiàn)為一個(gè)單件,在Application_Start的時(shí)候調(diào)用DataBase.Subscribe()。

      現(xiàn)在系統(tǒng)是這樣的結(jié)構(gòu):

      以下為引用的內(nèi)容:
      [img]http://images.cnblogs.com/cnblogs_com/yuyijq/aspnetpattrern2.jpg[/img]

      可以設(shè)想以后還會(huì)有更多的訂閱者。果然,昨天客戶要求在下一個(gè)版本中管理員分角色,各個(gè)角色看到的數(shù)據(jù)不同的,只有超級(jí)管理員才可以監(jiān)視所有數(shù)據(jù),OMG,呵呵,不過還好,我只需要添加幾個(gè)訂閱者就可以輕松搞定。

      后記:也許是我的文章標(biāo)題沒有起好,也許很多人得到模式恐懼癥,提到模式總是要來考察一下你的case,不是那種Enterprise級(jí)別的用了pattern就是過火了。其實(shí)這篇文章的內(nèi)容里沒有一點(diǎn)模式的氣息,只是用.net的Event實(shí)現(xiàn)觀察者模式的思想,我想如果合適,今天模式的投資,明天你會(huì)有收獲的。

      在.net里面我們有事件(event),那就無需使用傳統(tǒng)的觀察者模式的模型了

      那么我首先實(shí)現(xiàn)一個(gè)Monitor類,這個(gè)類用來接收客戶端傳遞來的數(shù)據(jù)并將數(shù)據(jù)廣播出去

      以下為引用的內(nèi)容:
      [img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
      public class DataEventArgs : EventArgs
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img]
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif[/img]
      [img]http://www.cnblogs.com/Images/dot.gif[/img]{
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      public string Message
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
      [img]http://www.cnblogs.com/Images/dot.gif[/img]{get;set;}
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      public DataEventArgs(string message)
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
      [img]http://www.cnblogs.com/Images/dot.gif[/img]{
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      this.Message = message;
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
      }
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img]
      }
      [img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
      public class Monitor
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img]
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif[/img]
      [img]http://www.cnblogs.com/Images/dot.gif[/img]{
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      public event EventHandler<DataEventArgs> DataIn;
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      private void SendData(string message)
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
      [img]http://www.cnblogs.com/Images/dot.gif[/img]{
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      if (DataIn != null)
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
      [img]http://www.cnblogs.com/Images/dot.gif[/img]{
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      DataEventArgs e = new DataEventArgs(message);
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      DataIn(this, e);
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
      }
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
      }
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
      /**//// <summary>
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      /// 這個(gè)方法被一個(gè)HttpHandler調(diào)用,客戶端向這個(gè)Handler發(fā)送數(shù)據(jù)
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      /// 數(shù)據(jù)處理后作為字符串傳遞給該方法,該方法然后將數(shù)據(jù)廣播出去
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      /// </summary>
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
      /// <param name="message">處理后的數(shù)據(jù)</param>
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      public void ReciveData(string message)
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img]
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img]
      [img]http://www.cnblogs.com/Images/dot.gif[/img]{
      [img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
      SendData(message);
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
      }
      [img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img]}
      [img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]

      分享:.Net教程之HTTP狀態(tài)碼200,301,302
      跳轉(zhuǎn)非常常用,在哪里都一樣,這里的一些說明和用法也如此,不止適用于asp.net,其他語言也會(huì)用得到。跳轉(zhuǎn)的目的本來很簡(jiǎn)單,就是當(dāng)用戶或系統(tǒng)需要時(shí)從一個(gè)頁面轉(zhuǎn)向另一個(gè)頁面,但自從有了

      共3頁上一頁123下一頁
      來源:模板無憂//所屬分類:.Net教程/更新時(shí)間:2009-08-01
      相關(guān).Net教程