二十四种设计模式:观察者模式(Observer Pattern)
观察者模式(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; /// <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