觀察者模式是一種可以描述一對多對象依賴關系的行為模式。當一個對象狀態(tài)發(fā)生變化時,依賴它的其它對象會自動被更新狀態(tài)。下面這個圖展示了觀察者模式的層級:
舉個例子吧,我們某個報表界面現(xiàn)在有個dashboard的數(shù)據(jù)發(fā)生變化,需要在它更新的時候去更新此頁面上其它幾個報表的數(shù)據(jù)那么可以使用觀察者模式來實現(xiàn)。觀察者模式有個很好的特點是能夠對觀察者的add/remve有很大的靈活性。
如果你對觀察者模式還是不很了解,建議你看看這里這篇文章:
http://blog.csdn.net/dujingjing1230/archive/2009/08/10/4430778.aspx
http://blog.csdn.net/dujingjing1230/archive/2009/08/12/4438348.aspx
我假設你是個framework1.1的使用者,對2.0的Generics還不清楚,那么你必須先去了解了解Generics這個類庫的基本使用。推薦你看看這個視頻:
http://www.microsoft.com/uk/msdn/nuggets/nugget/128/Generics-in-NET-Framework-20.aspx
英文還可以的話建議你看看這本書:
我很喜歡這本書。差不多50$。。
如果你和我一樣比較窮(上面的書是公司買的我慶幸。。。)最基本的是看這幾篇文章吧:
http://www.codeproject.com/KB/cs/generics_explained.aspx
http://msdn.microsoft.com/zh-cn/library/system.collections.generic.aspx
Anyway, 你應該去學學它。
言歸正傳.現(xiàn)在舉個例子,就是上面說的一個dashboard得數(shù)據(jù)發(fā)生變化時下面的報表需要跟著更新。這是說明觀察者模式特經典的一個例子。
1. 創(chuàng)建一個asp.net的web應用程序。
2. 創(chuàng)建一個主題類(DashboardPage)然后添加它需要的屬性和方法。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for DashboardPage
/// </summary>
public class DashboardPage : System.Web.UI.Page {
private List<IReport> _ReportCollection = new List<IReport>();
public string SelectedProduct { get; set; }
public DateTime SelectedDate { get; set; }
public DashboardPage() {
}
public void Add(IReport module) {
_ReportCollection.Add(module);
}
public void Remove(IReport module) {
_ReportCollection.Remove(module);
}
public void Update() {
foreach (IReport m in _ReportCollection) {
m.Update(this);
}
}
這個主題類將會作為參數(shù)傳遞給每個報表。
3. 創(chuàng)建一個觀察者接口來添加Update方法的定義。所有的觀察者類都需要繼承這個接口。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
/// <summary>
/// Summary description for IReport
/// </summary>
public interface IReport
{
void Update(DashboardPage page);
}
使用這個接口而不是直接在每個觀察者的類中把DashboardPage作為它們的參數(shù)是為了讓我們的實現(xiàn)更加靈活。
4. 添加一個SalesDashboard.aspx頁面讓它的后臺代碼繼承DashboardPage類。然后為它添加一個dropdownlist和一個calendar。
5. 創(chuàng)建兩個webUserControls作為兩個報表,一定記住需要繼承接口IReport。Update方法的實例化:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class SalesReport2 : System.Web.UI.UserControl, IReport
{
protected void Page_Load(object sender, EventArgs e)
{
}
#region IReport Members
public void Update(DashboardPage page) {
this.Label1.Text = page.SelectedProduct;
this.Label2.Text = page.SelectedDate.ToLongDateString();
}
#endregion
}
前臺:
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SalesReport2.ascx.cs" Inherits="SalesReport2" %>
<h2>Sales Report 2</h2>
Sales data for the product: <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<br />
Updated Date: <asp:Label ID="Label2" runat="server" Text=""></asp:Label>
6. 最后一步就是在Salesdasnboard.aspx頁面加載時需要添加吧兩個報表都添加到List<IReport>中。以便在Update的時候這兩個Report都能執(zhí)行Update(DashboardPage)方法。
protected override void OnLoad(EventArgs e) {
SelectedProduct = this.DropDownList1.SelectedValue;
SelectedDate = this.Calendar1.SelectedDate;
Add(SalesReport11);
Add(SalesReport21);
base.OnLoad(e);
}
7. 運行結果:
附上代碼:
http://download.csdn.net/source/1652674
如對本文有疑問,請?zhí)峤坏浇涣髡搲?,廣大熱心網(wǎng)友會為你解答?。?點擊進入論壇