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