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

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:解读Asp.net教程:设计IP地址屏蔽功能
出于安全考虑,几乎每个动态网站都具备IP地址屏蔽功能,而网上流传的很多关于该功能的教程大都采用字符串保存和验证IP地址,我认为这是不太科学的,我试图找到最佳的设计方案。 “I

经过一番思考后我还是回到我自己的Solution上,不过我又有了新的看法了。不是要将数据先缓存么?看看这个缓存,实际上她也是个观察者,至于她执行怎样的缓存策略是她的事情,如是我又有了一个新类:

以下为引用的内容:

//这里的代码接上篇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)
{
//将数据添加到数据库
}
}
}
}

PS:由于系统中我们只需要这样唯一一个订阅者,所以我将其实现为一个单件,在Application_Start的时候调用DataBase.Subscribe()。

现在系统是这样的结构:

以下为引用的内容:
[img]http://images.cnblogs.com/cnblogs_com/yuyijq/aspnetpattrern2.jpg[/img]

可以设想以后还会有更多的订阅者。果然,昨天客户要求在下一个版本中管理员分角色,各个角色看到的数据不同的,只有超级管理员才可以监视所有数据,OMG,呵呵,不过还好,我只需要添加几个订阅者就可以轻松搞定。

后记:也许是我的文章标题没有起好,也许很多人得到模式恐惧症,提到模式总是要来考察一下你的case,不是那种Enterprise级别的用了pattern就是过火了。其实这篇文章的内容里没有一点模式的气息,只是用.net的Event实现观察者模式的思想,我想如果合适,今天模式的投资,明天你会有收获的。

在.net里面我们有事件(event),那就无需使用传统的观察者模式的模型了

那么我首先实现一个Monitor类,这个类用来接收客户端传递来的数据并将数据广播出去

以下为引用的内容:
[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]
/// 这个方法被一个HttpHandler调用,客户端向这个Handler发送数据
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
/// 数据处理后作为字符串传递给该方法,该方法然后将数据广播出去
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
/// </summary>
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
/// <param name="message">处理后的数据</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状态码200,301,302
跳转非常常用,在哪里都一样,这里的一些说明和用法也如此,不止适用于asp.net,其他语言也会用得到。跳转的目的本来很简单,就是当用户或系统需要时从一个页面转向另一个页面,但自从有了

共3页上一页123下一页
来源:模板无忧//所属分类:.Net教程/更新时间:2009-08-01
相关.Net教程