访问者模式(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;
/// <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; }
}
}
}
AbstractElement
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Visitor
{
/// <summary>
/// 抽象元素(Element)
/// </summary>
public abstract class AbstractElement
{
/// <summary>
/// 执行抽象访问者的Visit()方法(从而执行抽象元素的方法)
/// </summary>
/// <param name="abstractVisitor">抽象访问者</param>
/// <returns></returns>
public abstract string Accept(AbstractVisitor abstractVisitor);
}
}
Message
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Visitor
{
/// <summary>
/// 操作Message抽象类(Element)
/// </summary>
public abstract class Message : AbstractElement
{
private MessageModel _messageModel; /// <summary>
/// 构造函数
/// </summary>
/// <param name="mm">Message实体对象</param>
public Message(MessageModel mm)
{
this._messageModel = mm;
} /// <summary>
/// Message实体对象
/// </summary>
public MessageModel MessageModel
{
get { return _messageModel; }
set { _messageModel = value; }
} /// <summary>
/// 执行抽象访问者的Visit()方法(从而执行抽象元素的方法)
/// </summary>
/// <param name="abstractVisitor">抽象访问者</param>
/// <returns></returns>
public override string Accept(AbstractVisitor abstractVisitor)
{
return abstractVisitor.Visit(this);
} /// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public abstract List<MessageModel> Get(); /// <summary>
/// 插入Message
/// </summary>
/// <returns></returns>
public abstract bool Insert();
}
}
SqlMessage
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Visitor
{
/// <summary>
/// Sql方式操作Message(ConcreteElement)
/// </summary>
public class SqlMessage : Message
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="mm">Message实体对象</param>
public SqlMessage(MessageModel mm)
: base(mm)
{ } /// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public override List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("SQL方式获取Message", DateTime.Now)); return l;
} /// <summary>
/// 插入Message
/// </summary>
/// <returns></returns>
public override bool Insert()
{
// 代码略
return true;
}
}
}
XmlMessage
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Visitor
{
/// <summary>
/// Xml方式操作Message(ConcreteElement)
/// </summary>
public class XmlMessage : Message
{
/// <summary>
/// 构造函数
/// </summary>
/// <param name="mm">Message实体对象</param>
public XmlMessage(MessageModel mm)
: base(mm)
{ } /// <summary>
/// 获取Message
/// </summary>
/// <returns></returns>
public override List<MessageModel> Get()
{
List<MessageModel> l = new List<MessageModel>();
l.Add(new MessageModel("XML方式获取Message", DateTime.Now)); return l;
} /// <summary>
/// 插入Message
/// </summary>
/// <returns></returns>
public override bool Insert()
{
// 代码略
return true;
}
}
}
AbstractVisitor
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Visitor
{
/// <summary>
/// 抽象访问者(Visitor)
/// </summary>
public abstract class AbstractVisitor
{
/// <summary>
/// 执行抽象元素的方法
/// </summary>
/// <param name="abstractElement">抽象元素</param>
/// <returns></returns>
public abstract string Visit(AbstractElement abstractElement);
}
}
InsertVisitor
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Visitor
{
/// <summary>
/// Insert访问者(ConcreteVisitor)
/// </summary>
public class InsertVisitor : AbstractVisitor
{
/// <summary>
/// 执行Message的Insert()方法
/// </summary>
/// <param name="abstractElement">抽象元素</param>
/// <returns></returns>
public override string Visit(AbstractElement abstractElement)
{
Message m = abstractElement as Message; return m.Insert().ToString() + "<br />";
}
}
}
GetVisitor
using System;
using System.Collections.Generic;
using System.Text; namespace Pattern.Visitor
{
/// <summary>
/// Get访问者(ConcreteVisitor)
/// </summary>
public class GetVisitor : AbstractVisitor
{
/// <summary>
/// 执行Message的Get()方法
/// </summary>
/// <param name="abstractElement">抽象元素</param>
/// <returns></returns>
public override string Visit(AbstractElement abstractElement)
{
Message m = abstractElement as Message; return m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString() + "<br />";
}
}
}
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