Messages
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Visitor
{
/**//// <summary>
/// Message集合(ObjectStructure)
/// </summary>
public class Messages
{
private List<Message> _list = new List<Message>();
/**//// <summary>
/// 添加一個Message對象
/// </summary>
/// <param name="message">Message對象</param>
public void Attach(Message message)
{
_list.Add(message);
}
/**//// <summary>
/// 移除一個Message對象
/// </summary>
/// <param name="message">Message對象</param>
public void Detach(Message message)
{
_list.Remove(message);
}
/**//// <summary>
/// 執行集合內所有Message對象的Accept()方法(執行抽象訪問者的Visit()方法(從而執行抽象元素的方法))
/// </summary>
/// <param name="abstractVisitor">抽象訪問者</param>
/// <returns></returns>
public string Accept(AbstractVisitor abstractVisitor)
{
string s = "";
foreach (Message m in _list)
{
s += m.Accept(abstractVisitor);
}
return s;
}
}
}
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.Visitor;
public partial class Visitor : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Pattern.Visitor.Messages m = new Pattern.Visitor.Messages();
m.Attach(new SqlMessage(new MessageModel("插入", DateTime.Now)));
m.Attach(new XMLMessage(new MessageModel("插入", DateTime.Now)));
Response.Write(m.Accept(new InsertVisitor()));
Response.Write(m.Accept(new GetVisitor()));
}
}
運行結果
True
True
SQL方式獲取Message 2007-5-27 15:01:53
XML方式獲取Message 2007-5-27 15:01:53
參考
http://www.dofactory.com/Patterns/PatternVisitor.ASPx
OKa