在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的,详细内容请参见框架程序设计那本书)
|