什么叫观察者设计模式(Observer Pattern),在此不作过多解释。
Insus.NET以一个很简单的例子来演示给大家看看。一个是发布者,而发布者只关心会有谁订阅:
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 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:
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:
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:
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)之中互动,重点部分有注释。
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收到消息之后,回复答谢,当然也使用相同的方法了。
public delegate void UserControlEventHandler(object sender, string message);
UserD的告诉UserC方法:
{
if (Click != null)
{
string message = "Hi,UserC, 有好消息告诉你,"+ this.Label1.Text + "开始有销售了,你也去购买一本吧。";
Click(this, message);
}
this.Button1.Visible = false;
}
Page对象处理委托的事情:
{
base.OnInit(e);
this.UserD1.Click += new UserD.UserControlEventHandler(this.UserC1.Tell);
}
UserC接收到消息之后,需要处理的事情:
{
this.UsercInfo.Visible = true;
this.Label1.Text = message;
}
下面可以看到动画效果:
下面是演示代码:
http://download.cnblogs.com/insus/ASPDOTNET/ObserverAndUserControlInteractive.rar