返回“設計模式(C#)系列文章索引”
介紹
定義對象間的一種一對多的依賴關系,以便當一個對象的狀態發生改變時,所有依賴於它的對象都得到通知並自動刷新。
示例
有一個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;
/**//// <summary>
/// Message內容
/// </summary>
public string Message
{
get { return _message; }
set { _message = value; }
}
private DateTime _publishTime;
/**//// <summary>
/// Message發布時間
/// </summary>
public DateTime PublishTime
{
get { return _publishTime; }
set { _publishTime = value; }
}
}
}
IMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Observer
{
/**//// <summary>
/// 操作Message的接口(Observer)
/// </summary>
public interface IMessage
{
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
string Insert(MessageModel mm);
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Observer
{
/**//// <summary>
/// Sql方式操作Message(ConcreteObserver)
/// </summary>
public class SqlMessage : IMessage
{
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
public string Insert(MessageModel mm)
{
return "sql方式:" + mm.Message + " " + mm.PublishTime.ToString();
}
}
}
XmlMessage
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Observer
{
/**//// <summary>
/// Xml方式操作Message(ConcreteObserver)
/// </summary>
public class XmlMessage : IMessage
{
/**//// <summary>
/// 插入Message
/// </summary>
/// <param name="mm">Message實體對象</param>
/// <returns></returns>
public string Insert(MessageModel mm)
{
return "xml方式:" + mm.Message + " " + mm.PublishTime.ToString();
}
}
}
AbstractMessageSubject
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Observer
{
/**//// <summary>
/// 抽象Message主題(Subject)
/// </summary>
public abstract class AbstractMessageSubject
{
private MessageModel _messageModel;
private List<IMessage> list = new List<IMessage>();
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="mm">Message實體對象</param>
public AbstractMessageSubject(MessageModel mm)
{
this._messageModel = mm;
}
/**//// <summary>
/// 添加一個實現IMessage接口的對象
/// </summary>
/// <param name="m">實現IMessage接口的對象</param>
public void Attach(IMessage m)
{
list.Add(m);
}
/**//// <summary>
/// 移除一個實現IMessage接口的對象
/// </summary>
/// <param name="m">實現IMessage接口的對象</param>
public void Detach(IMessage m)
{
list.Remove(m);
}
/**//// <summary>
/// 通知所有觀察者
/// </summary>
/// <returns></returns>
public string Notify()
{
string s = "";
foreach (IMessage i in list)
{
s += i.Insert(_messageModel);
s += "<br />";
}
return s;
}
/**//// <summary>
/// Message發布時間
/// </summary>
public DateTime PublishTime
{
get { return _messageModel.PublishTime; }
set
{
_messageModel.PublishTime = value;
Notify();
}
}
}
}
MessageSubject
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Observer
{
/**//// <summary>
/// Message主題(ConcreteSubject)
/// </summary>
public class MessageSubject : AbstractMessageSubject
{
/**//// <summary>
/// 構造函數
/// </summary>
/// <param name="mm">Message實體對象</param>
public MessageSubject(MessageModel mm)
: base(mm)
{
}
}
}
Test
using System;
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
參考
http://www.dofactory.com/Patterns/PatternObserver.aspx
OK