什么叫观察者设计模式(Observer Pattern),在此不作过多解释。Insus.NET以一个很简单的例子来演示给大家看看。一个是发布者,而发布者只关心会有谁订阅:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IPublish
/// </summary>
namespace Insus.NET
{
// 发布者接口
public interface IPublish
{
// 只关心会有谁订阅
void Subscription(ISubscribe obj);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IPublish
/// </summary>
namespace Insus.NET
{
// 发布者接口
public interface IPublish
{
// 只关心会有谁订阅
void Subscription(ISubscribe obj);
}
}
另一个是订阅者,而只会关心自己订阅的,是否发布的事情:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ISubscribe
/// </summary>
namespace Insus.NET
{
// 订阅者接口
public interface ISubscribe
{
// 订阅者只会关心自己订阅发布的事情
void Release( string bookName);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for ISubscribe
/// </summary>
namespace Insus.NET
{
// 订阅者接口
public interface ISubscribe
{
// 订阅者只会关心自己订阅发布的事情
void Release( string bookName);
}
}
下面,Insus.NET列出四个用户,均以用户控件作代表,分别是UserA.ascx,UserB.ascx,UserC.ascx和UserD.ascx ,一旦发布者发布消息之后,订阅者会收到信息,并做出自己的事情。
其中UserA,UserB和UserD作了订阅,也就是实作了ISubscribe接口,而UserC没有订阅,所以它没有实作接口。
UserA.ascx.cs:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class UserA : System.Web.UI.UserControl,ISubscribe
{
protected void Page_Load( object sender, EventArgs e)
{
}
public void Release( string bookName)
{
Label label = new Label();
label.ForeColor = Color.Red;
label.Text = " UserA: 我订阅的书 " + bookName + " 发行了。 ";
this.PlaceHolder1.Controls.Add(label);
}
}
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class UserA : System.Web.UI.UserControl,ISubscribe
{
protected void Page_Load( object sender, EventArgs e)
{
}
public void Release( string bookName)
{
Label label = new Label();
label.ForeColor = Color.Red;
label.Text = " UserA: 我订阅的书 " + bookName + " 发行了。 ";
this.PlaceHolder1.Controls.Add(label);
}
}
UserB.ascx.cs:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class UserB : System.Web.UI.UserControl, ISubscribe
{
protected void Page_Load( object sender, EventArgs e)
{
}
public void Release( string bookName)
{
this.Label1.Visible = true;
this.Label1.Text = " UserB: 马上去书店购买 " + bookName;
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class UserB : System.Web.UI.UserControl, ISubscribe
{
protected void Page_Load( object sender, EventArgs e)
{
}
public void Release( string bookName)
{
this.Label1.Visible = true;
this.Label1.Text = " UserB: 马上去书店购买 " + bookName;
}
}
UserD.ascx.cs:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class UserD : System.Web.UI.UserControl,ISubscribe
{
protected void Page_Load( object sender, EventArgs e)
{
}
public void Release( string bookName)
{
this.myInfo.Visible = true;
this.Label1.Text = bookName;
}
}
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class UserD : System.Web.UI.UserControl,ISubscribe
{
protected void Page_Load( object sender, EventArgs e)
{
}
public void Release( string bookName)
{
this.myInfo.Visible = true;
this.Label1.Text = bookName;
}
}
不管怎样,它们之间必须在一个平台aspx(Default.aspx)之中互动,重点部分有注释。
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page, IPublish
{
// 用一个集合,存储订阅者
List<ISubscribe> _List = new List<ISubscribe>();
string bookName = " 《观察者设计模式诠释》 ";
protected void Page_Load( object sender, EventArgs e)
{
// 在这里登记订阅的用户
this.Subscription((ISubscribe) this.UserA1);
this.Subscription((ISubscribe) this.UserB1);
this.Subscription((ISubscribe) this.UserD1);
this.LabelBookName.Text = bookName;
}
protected void Button1_Click( object sender, EventArgs e)
{
// 这样好的书,竟然无人订阅!!!
if (_List.Count == 0) return;
// 当有订阅者时,把消息发布给他们。
for ( int i = 0; i < _List.Count; i++)
{
((ISubscribe)_List[i]).Release(bookName);
}
}
public void Subscription(ISubscribe obj)
{
_List.Add(obj);
}
}
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Insus.NET;
public partial class _Default : System.Web.UI.Page, IPublish
{
// 用一个集合,存储订阅者
List<ISubscribe> _List = new List<ISubscribe>();
string bookName = " 《观察者设计模式诠释》 ";
protected void Page_Load( object sender, EventArgs e)
{
// 在这里登记订阅的用户
this.Subscription((ISubscribe) this.UserA1);
this.Subscription((ISubscribe) this.UserB1);
this.Subscription((ISubscribe) this.UserD1);
this.LabelBookName.Text = bookName;
}
protected void Button1_Click( object sender, EventArgs e)
{
// 这样好的书,竟然无人订阅!!!
if (_List.Count == 0) return;
// 当有订阅者时,把消息发布给他们。
for ( int i = 0; i < _List.Count; i++)
{
((ISubscribe)_List[i]).Release(bookName);
}
}
public void Subscription(ISubscribe obj)
{
_List.Add(obj);
}
}
由于UserC没有订阅,因此UserD用户收到发布消息之后,会告诉UserC.但是又由于UserD与UserC没有面对面,只有通过另外一个方式,打电话或委托(电话,或是委托对象,就是Page)的方式来告之了。当UserC收到消息之后,回复答谢,当然也使用相同的方法了。
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
//
由于不能面对面告之,只好使用另外一种方法,如委委托Page去做。
public delegate void UserControlEventHandler( object sender, string message);
public delegate void UserControlEventHandler( object sender, string message);
UserD的告诉UserC方法:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
protected
void Button1_Click(
object sender, EventArgs e)
{
if (Click != null)
{
string message = " Hi,UserC, 有好消息告诉你, "+ this.Label1.Text + " 开始有销售了,你也去购买一本吧。 ";
Click( this, message);
}
this.Button1.Visible = false;
}
{
if (Click != null)
{
string message = " Hi,UserC, 有好消息告诉你, "+ this.Label1.Text + " 开始有销售了,你也去购买一本吧。 ";
Click( this, message);
}
this.Button1.Visible = false;
}
Page对象处理委托的事情:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
protected
override
void OnInit(EventArgs e)
{
base.OnInit(e);
this.UserD1.Click += new UserD.UserControlEventHandler( this.UserC1.Tell);
}
{
base.OnInit(e);
this.UserD1.Click += new UserD.UserControlEventHandler( this.UserC1.Tell);
}
UserC接收到消息之后,需要处理的事情:
![](https://i-blog.csdnimg.cn/blog_migrate/cdec0645add3fc3c328197dda5c76203.gif)
public
void Tell(
object sender,
string message)
{
this.UsercInfo.Visible = true;
this.Label1.Text = message;
}
{
this.UsercInfo.Visible = true;
this.Label1.Text = message;
}
下面可以看到动画效果:
![](https://img-my.csdn.net/uploads/201209/06/1346940891_9473.gif)