樂在其中設計模式(C#) - 觀察者模式(Observer Pattern)
介紹
定義對象間的一種一對多的依賴關系,以便當一個對象的狀态發生改變時,所有依賴于它的對象都得到通知并自動重新整理。
示例
有一個Message實體類,某些對象對它的操作有Insert()方法,當發生某些改變的時候,通知所有這些對象并執行Insert()方法。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Observer
{
/**//// <summary>
/// Message實體類
/// </summary>
public class MessageModel
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="msg">Message内容</param>
/// <param name="pt">Message釋出時間</param>
public MessageModel(string msg, DateTime pt)
this._message = msg;
this._publishTime = pt;
}
private string _message;
/// Message内容
public string Message
get
{ return _message; }
set
{ _message = value; }
private DateTime _publishTime;
/// Message釋出時間
public DateTime PublishTime
{ return _publishTime; }
{ _publishTime = value; }
}
}
IMessage
/// 操作Message的接口(Observer)
public interface IMessage
/// 插入Message
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
string Insert(MessageModel mm);
SqlMessage
/// Sql方式操作Message(ConcreteObserver)
public class SqlMessage : IMessage
public string Insert(MessageModel mm)
return "sql方式:" + mm.Message + " " + mm.PublishTime.ToString();
}
XmlMessage
/// Xml方式操作Message(ConcreteObserver)
public class XmlMessage : IMessage
return "xml方式:" + mm.Message + " " + mm.PublishTime.ToString();
AbstractMessageSubject
/// 抽象Message主題(Subject)
public abstract class AbstractMessageSubject
private MessageModel _messageModel;
private List<IMessage> list = new List<IMessage>();
public AbstractMessageSubject(MessageModel mm)
this._messageModel = mm;
/// 添加一個實作IMessage接口的對象
/// <param name="m">實作IMessage接口的對象</param>
public void Attach(IMessage m)
list.Add(m);
/// 移除一個實作IMessage接口的對象
public void Detach(IMessage m)
list.Remove(m);
/// 通知所有觀察者
public string Notify()
string s = "";
foreach (IMessage i in list)
s += i.Insert(_messageModel);
s += "<br />";
}
return s;
{ return _messageModel.PublishTime; }
set
_messageModel.PublishTime = value;
Notify();
MessageSubject
/// Message主題(ConcreteSubject)
public class MessageSubject : AbstractMessageSubject
public MessageSubject(MessageModel mm)
: base(mm)
Test
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Pattern.Observer;
public partial class Observer : System.Web.UI.Page
protected void Page_Load(object sender, EventArgs e)
MessageSubject m = new MessageSubject(new MessageModel("插入Message", DateTime.Now));
SqlMessage sqlMessage = new SqlMessage();
XmlMessage xmlMessage = new XmlMessage();
m.Attach(sqlMessage);
m.Attach(xmlMessage);
// System.Threading.Thread.Sleep(1000);
// 修改了PublishTime就會通知所有觀察者
// m.PublishTime = DateTime.Now;
Response.Write(m.Notify());
運作結果
sql方式:插入Message 2007-5-26 9:38:18
xml方式:插入Message 2007-5-26 9:38:18
參考
<a href="http://www.dofactory.com/Patterns/PatternObserver.aspx" target="_blank">http://www.dofactory.com/Patterns/PatternObserver.aspx</a>
OK
<a href="http://files.cnblogs.com/webabcd/Pattern.rar">[源碼下載下傳]</a>