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();
}
}
}
}