乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)
介绍
表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。
示例
有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要针对其中某一方法进行操作。
MessageModel
using System;
using System.Collections.Generic;
using System.Text;
namespace Pattern.Visitor
{
/**//// <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;
/// Message内容
public string Message
get
{ return _message; }
set
{ _message = value; }
private DateTime _publishTime;
/// Message发布时间
public DateTime PublishTime
{ return _publishTime; }
{ _publishTime = value; }
}
}
AbstractElement
/// 抽象元素(Element)
public abstract class AbstractElement
/// 执行抽象访问者的Visit()方法(从而执行抽象元素的方法)
/// <param name="abstractVisitor">抽象访问者</param>
/// <returns></returns>
public abstract string Accept(AbstractVisitor abstractVisitor);
Message
/// 操作Message抽象类(Element)
public abstract class Message : AbstractElement
private MessageModel _messageModel;
/// <param name="mm">Message实体对象</param>
public Message(MessageModel mm)
this._messageModel = mm;
/// Message实体对象
public MessageModel MessageModel
{ return _messageModel; }
{ _messageModel = value; }
public override string Accept(AbstractVisitor abstractVisitor)
return abstractVisitor.Visit(this);
/// 获取Message
public abstract List<MessageModel> Get();
/// 插入Message
public abstract bool Insert();
SqlMessage
/// Sql方式操作Message(ConcreteElement)
public class SqlMessage : Message
public SqlMessage(MessageModel mm)
: base(mm)
public override List<MessageModel> Get()
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("SQL方式获取Message", DateTime.Now));
return l;
public override bool Insert()
// 代码略
return true;
XmlMessage
/// Xml方式操作Message(ConcreteElement)
public class XmlMessage : Message
public XmlMessage(MessageModel mm)
l.Add(new MessageModel("XML方式获取Message", DateTime.Now));
AbstractVisitor
/// 抽象访问者(Visitor)
public abstract class AbstractVisitor
/// 执行抽象元素的方法
/// <param name="abstractElement">抽象元素</param>
public abstract string Visit(AbstractElement abstractElement);
InsertVisitor
/// Insert访问者(ConcreteVisitor)
public class InsertVisitor : AbstractVisitor
/// 执行Message的Insert()方法
public override string Visit(AbstractElement abstractElement)
Message m = abstractElement as Message;
return m.Insert().ToString() + "<br />";
GetVisitor
/// Get访问者(ConcreteVisitor)
public class GetVisitor : AbstractVisitor
/// 执行Message的Get()方法
return m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString() + "<br />";
Messages
/// Message集合(ObjectStructure)
public class Messages
private List<Message> _list = new List<Message>();
/// 添加一个Message对象
/// <param name="message">Message对象</param>
public void Attach(Message message)
_list.Add(message);
/// 移除一个Message对象
public void Detach(Message message)
_list.Remove(message);
/// 执行集合内所有Message对象的Accept()方法(执行抽象访问者的Visit()方法(从而执行抽象元素的方法))
public string Accept(AbstractVisitor abstractVisitor)
string s = "";
foreach (Message m in _list)
s += m.Accept(abstractVisitor);
}
return s;
Test
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
SQL方式获取Message 2007-5-27 15:01:53
XML方式获取Message 2007-5-27 15:01:53
参考
<a href="http://www.dofactory.com/Patterns/PatternVisitor.aspx" target="_blank">http://www.dofactory.com/Patterns/PatternVisitor.aspx</a>
OK
<a href="http://files.cnblogs.com/webabcd/Pattern.rar">[源码下载]</a>