推荐:解读Asp.net教程:设计IP地址屏蔽功能出于安全考虑,几乎每个动态网站都具备IP地址屏蔽功能,而网上流传的很多关于该功能的教程大都采用字符串保存和验证IP地址,我认为这是不太科学的,我试图找到最佳的设计方案。“I
在asp.net中实现观察者模式?难道asp.net中的观察者模式有什么特别么?嗯,基于Http协议的Application难免有些健忘,我是这样实现的,不知道有没有更好的办法?
先谈谈需求吧,以免陷入空谈
最近一个Case, 这样的需求:很多客户端不断的向Web Application提交数据,管理员进入Web的管理页面可以即时的看到这些数据,有多个管理员可以同时浏览,且管理员浏览的数据从管理员开始监视那个时刻起,不能显示以前的数据。从这个场景一看,明显的观察者模式,管理员开始监视时,订阅数据,数据到达的时候向所有订阅了数据的管理员广播数据。
需求如下图:
有了发布者还需要订阅者,我们实现管理员类,来订阅数据
以下为引用的内容: public class Admin { /**//// <summary> /// 用这个保存所有收到的数据 /// </summary> public IList<string> MessageList { get; set; } public Admin(Monitor monitor) { MessageList = new List<string>(); monitor.DataIn = new EventHandler< DataEventArgs>(ReciveMessage); } [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] private void ReciveMessage(object sender, DataEventArgs e) [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img] [img]http://www.VeVb.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img] [img]http://www.VeVb.com/Images/dot.gif[/img]{ [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] MessageList.Add(e.Message); [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] } [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img] } [img]http://www.VeVb.com/Images/OutliningIndicators/None.gif[/img] |
Ok,需要具备的元素我们都写好了,但是如何让它们工作起来?如果使Winform程序,那将毫无悬念。
分析:我们碰到的问题
第一个问题:当客户端发送一个数据包,我们是实例化一个新的Monitor么?如果是,哪么每次实例化一个全新的Monitor,所有在它上面订阅的事件将全部消失了,如果不是那这个Monitor将如何存在呢?总不能真空吧,两个http请求之间如何保存数据呢?不过再把需求一读,好像整个应用程序中就只需要也只能有一个这样的Monitor呢,该是单件模式上场的时候了。
在上面的Monitor的实现中添加下面的代码:
以下为引用的内容: [img]http://www.VeVb.com/Images/OutliningIndicators/None.gif[/img] private static Monitor _instance = null; [img]http://www.VeVb.com/Images/OutliningIndicators/None.gif[/img]public static Monitor Current [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img] [img]http://www.VeVb.com/Images/OutliningIndicators/ContractedBlock.gif[/img] [img]http://www.VeVb.com/Images/dot.gif[/img]{ [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] get [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img] [img]http://www.VeVb.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img] [img]http://www.VeVb.com/Images/dot.gif[/img]{ [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] if (_instance == null) [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] _instance = new Monitor(); [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] return _instance; [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] } [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img]} |
但是本系统存在多个客户端,所以为了避免多线程造成问题,还是来Double Check一下吧,修改上面的代码如下:
以下为引用的内容: [img]http://www.VeVb.com/Images/OutliningIndicators/None.gif[/img] public static Monitor Current [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedBlockStart.gif[/img] [img]http://www.VeVb.com/Images/OutliningIndicators/ContractedBlock.gif[/img] [img]http://www.VeVb.com/Images/dot.gif[/img]{ [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] get [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img] [img]http://www.VeVb.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img] [img]http://www.VeVb.com/Images/dot.gif[/img]{ [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] object o = new object(); [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] if (_instance == null) [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img] [img]http://www.VeVb.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img] [img]http://www.VeVb.com/Images/dot.gif[/img]{ [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] lock (o) [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif[/img] [img]http://www.VeVb.com/Images/OutliningIndicators/ContractedSubBlock.gif[/img] [img]http://www.VeVb.com/Images/dot.gif[/img]{ [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] if (_instance == null) [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] _instance = new Monitor(); [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] } [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] } [img]http://www.VeVb.com/Images/OutliningIndicators/InBlock.gif[/img] return _instance; [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img] } [img]http://www.VeVb.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img] } [img]http://www.VeVb.com/Images/OutliningIndicators/None.gif[/img] |
(PS:为什么使用单件就可以跨请求保存实例了呢?因为这里使用了一个static member保存Monitor的引用,static member在.net的GC里面是被作为Root的,详细内容请参见框架程序设计那本书)
分享:.Net教程之HTTP状态码200,301,302跳转非常常用,在哪里都一样,这里的一些说明和用法也如此,不止适用于asp.net,其他语言也会用得到。跳转的目的本来很简单,就是当用户或系统需要时从一个页面转向另一个页面,但自从有了
新闻热点
疑难解答
图片精选