网站建设
  简约型网页设计套餐998
  实惠型网站建设套餐2580
  综合型网站制作套餐4980
  网站改版与网站维护
  行业网站建设方案
  大型网站建设解决方案
  企业网站建设流程
  帝网科技网站设计与网站制作
建站FAQ
·网站空间问题解答
·企业邮箱问题解答
 
酷站欣赏
·房产酷站(379)
·综合门户(8 9)
·建筑装饰(603)
·手机通讯(354)
·生活购物(376)
·医疗保健(199)
·文化摄影(602)
·休闲体育(399)
>>更多酷站欣赏
网站优化
·Google(谷歌)优化   ·百度(BaiDu)优化
·雅虎(Yahoo)优化    ·Alexa排名优化   
·Google AdSense   ·DMOZ目录提交  
建站知识
·网站建设知识·网站名词解释·网站运营知识
·网络营销知识·搜索引擎知识·实用技术文摘
网站推广
百度网站推广 google网站推广
搜狐网站推广 网易网站推广
新浪网站推广   雅虎网站推广
  您当前位置: 当前位置:帝网科技 >> web开发 >> .NET专栏 >> 浏览文章
 
 
asp.net中的观察者模式
作者:乐乐整理 来源:帝网科技 日期:2008年09月16日 点击数:



在asp.net中实现观察者模式?难道asp.net中的观察者模式有什么特别么?嗯,基于Http协议的Application难免有些健忘,我是这样实现的,不知道有没有更好的办法?

先谈谈需求吧,以免陷入空谈

最近一个Case, 这样的需求:很多客户端不断的向Web Application提交数据,管理员进入Web的管理页面可以即时的看到这些数据,有多个管理员可以同时浏览,且管理员浏览的数据从管理员开始监视那个时刻起,不能显示以前的数据。从这个场景一看,明显的观察者模式,管理员开始监视时,订阅数据,数据到达的时候向所有订阅了数据的管理员广播数据。

需求如下图:

点此在新窗口浏览图片 

有了发布者还需要订阅者,我们实现管理员类,来订阅数据

public class Admin
   {
     /**////


     /// 用这个保存所有收到的数据
     ///

     public IList MessageList
     { get; set; }
     public Admin(Monitor monitor)
     {
         MessageList = new List();
         monitor.DataIn += new EventHandler< DataEventArgs>(ReciveMessage);
     }
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
     private void ReciveMessage(object sender, DataEventArgs e)
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">{
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">        
MessageList.Add(e.Message);
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
     }
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
   }
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">

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

分析:我们碰到的问题

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

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

按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
private static Monitor _instance = null;
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">public static
Monitor Current
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">{
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
   get
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">  
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">{
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
     if (_instance == null)
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
       _instance = new Monitor();
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
     return _instance;
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
   }
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">}

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

按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
public static Monitor Current
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">    
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">{
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
         get
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
         按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">{
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">          
object o = new object();
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
           if (_instance == null)
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
           按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">{
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">            
lock (o)
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
             按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">{
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">                
if (_instance == null)
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
                   _instance = new Monitor();
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
             }
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
           }
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">          
return _instance;
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
         }
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">
     }
按此在新窗口浏览图片screen.width-333)this.width=screen.width-333">

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

  相关文章
 
·ASP.NET使用log4Net日志组件教程(日志
·ASP.NET MVC 框架
·C#实现的BinaryTree
·WebForms使用System.Web.Routing
·ASP.NET获取远程网页下载到本地文件
·一个“简单”的ASP.NET的服务器控件
·ASP.net与PHP两大网站开发架构优势对比
·教你七招提高.NET网站性能
·ASP.NET未来:简化开发 HTML5性能提升
·ASP.NET实现类似Excel的数据透视表
·FileUpload上传多文件出现错误的解决方
·.NET从优酷专辑中采集所有视频及信息(
·ASP.NET 4中的SEO改进
·详解Asp.net MVC DropDownLists
·提高ASP.NET应用程序性能的几招方法
·asp.net实现51job地区选择效果
·ASP.NET中创建GeoRSS订阅源
·ASP.NET 4.0开发更加简便
·ASP.NET页面间数据传递的方法
·ASP.NET的SEO:使用.ashx文件——排除
 
 

公司环境 | 合作伙伴 | 人才招聘 | 付款方式 | 关于我们

地址:广州市天河区中山大道中120号D805 电话:020-82529556 传真:020-82529556
广州帝网网络科技有限公司 版权所有 粤ICP备08119341号