a.net中的观察者模式.(1)[图]_.NET概论及软件使用_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

a.net中的观察者模式.(1)[图]

作者:黑客防线网安ASP维护基地 来源:黑客防线网安ASP维护基地 浏览次数:0

本篇关键词:模式 /imgMonitor
黑客防线网安网讯:    在asp.net中实现观察者模式?难道asp.net中的观察者模式有什么特别么?嗯,基于Http协议的Application难免有些健忘,我是这样实现的,不知道有没有更好的办法? 先谈谈需求吧,以免陷入...

    在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.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
     private void ReciveMessage(object sender, DataEventArgs e)
[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]        
MessageList.Add(e.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]

Ok,需要具备的元素我们都写好了,但是如何让它们工作起来?如果使Winform程序,那将毫无悬念。

分析:我们碰到的问题

第一个问题:当客户端发送一个数据包,我们是实例化一个新的Monitor么?如果是,哪么每次实例化一个全新的Monitor,所有在它上面订阅的事件将全部消失了,如果不是那这个Monitor将如何存在呢?总不能真空吧,两个http请求之间如何保存数据呢?不过再把需求一读,好像整个应用程序中就只需要也只能有一个这样的Monitor呢,该是单件模式上场的时候了。

在上面的Monitor的实现中添加下面的代码:

[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
private static Monitor _instance = null;
[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]public static
Monitor Current
[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]
   get
[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 (_instance == null)
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
       _instance = new Monitor();
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
     return _instance;
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif[/img]
   }
[img]http://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif[/img]}

但是本系统存在多个客户端,所以为了避免多线程造成问题,还是来Double Check一下吧,修改上面的代码如下:

[img]http://www.cnblogs.com/Images/OutliningIndicators/None.gif[/img]
public static Monitor Current
[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]
         get
[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]          
object o = new object();
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
           if (_instance == 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]            
lock (o)
[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 (_instance == null)
[img]http://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif[/img]
                   _instance = new Monitor();
[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/InBlock.gif[/img]          
return _instance;
[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]

(PS:为什么使用单件就可以跨请求保存实例了呢?因为这里使用了一个static member保存Monitor的引用,static member在.net的GC里面是被作为Root的,详细内容请参见框架程序设计那本书)

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-14078-1.html
网站维护教程更新时间:2012-03-30 05:26:00  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer