访问者模式(Vistor Pattern)
访问者模式(Vistor Pattern)是一种行为型模式,表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素类的前提下定义作用于这些元素的新操作。访问者模式适用于数据结构相对稳定算法又易变化的系统。因为访问者模式使得算法操作增加变得容易。若系统数据结构对象易于变化,经常有新的数据对象增加进来,则不适合使用访问者模式。访问者模式将有关行为集中到一个访问者对象中,其改变不影响系统数据结构。其缺点就是增加新的数据结构很困难。元素类可以通过接受不同的访问者来实现对不同操作的扩展。Visitor(访问者)模式使得增加新的操作变得容易,它可以收集有关联的方法,而分离没有关联的方法,特别适用于分离因为不同原因而变化的事物。
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication { //一般每个接口或类都写在单独的.cs文件中 //本示例为了执行查看方便才写在一起 /// <summary> /// <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; } } } /// <summary> /// 抽象元素(Element) /// </summary> public abstract class AbstractElement { /// <summary> /// 执行抽象访问者的Visit()方法(从而执行抽象元素的方法) /// </summary> /// <param name="abstractVisitor">抽象访问者</param> /// <returns></returns> public abstract string Accept(AbstractVisitor abstractVisitor); } /// <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(); } /// <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; } } /// <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; } } /// <summary> /// 抽象访问者(Visitor) /// </summary> public abstract class AbstractVisitor { /// <summary> /// 执行抽象元素的方法 /// </summary> /// <param name="abstractElement">抽象元素</param> /// <returns></returns> public abstract string Visit(AbstractElement abstractElement); } /// <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() + "\r\n"; } } /// <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() + "\r\n"; } } /// <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; } } class Program { static void Main(string[] args) { Messages m = new Messages(); m.Attach(new SqlMessage(new MessageModel("插入", DateTime.Now))); m.Attach(new XmlMessage(new MessageModel("插入", DateTime.Now))); Console.WriteLine(m.Accept(new InsertVisitor())); Console.WriteLine(m.Accept(new GetVisitor())); } } }