首页 > 其他 > 详细

乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

时间:2014-03-09 21:21:29      阅读:676      评论:0      收藏:0      [点我收藏+]
原文:乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

[索引页]
[源码下载]


乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)


作者:webabcd


介绍
表示一个作用于某对象结构中的各元素的操作。它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。


示例
有一个Message实体类,某些对象对它的操作有Insert()和Get()方法,现在要针对其中某一方法进行操作。
bubuko.com,布布扣


MessageModel
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// Message实体类
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class MessageModel
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="msg">Message内容</param>
bubuko.com,布布扣        
/// <param name="pt">Message发布时间</param>

bubuko.com,布布扣        public MessageModel(string msg, DateTime pt)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
this._message = msg;
bubuko.com,布布扣            
this._publishTime = pt;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
private string _message;
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message内容
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public string Message
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _message; }
bubuko.com,布布扣            
set { _message = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
private DateTime _publishTime;
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message发布时间
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public DateTime PublishTime
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _publishTime; }
bubuko.com,布布扣            
set { _publishTime = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

AbstractElement
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 抽象元素(Element)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public abstract class AbstractElement
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 执行抽象访问者的Visit()方法(从而执行抽象元素的方法)
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="abstractVisitor">抽象访问者</param>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public abstract string Accept(AbstractVisitor abstractVisitor);
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

Message
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 操作Message抽象类(Element)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public abstract class Message : AbstractElement
bubuko.com,布布扣    
{
bubuko.com,布布扣        
private MessageModel _messageModel;
bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="mm">Message实体对象</param>

bubuko.com,布布扣        public Message(MessageModel mm)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
this._messageModel = mm;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// Message实体对象
bubuko.com,布布扣        
/// </summary>

bubuko.com,布布扣        public MessageModel MessageModel
bubuko.com,布布扣        
{
bubuko.com,布布扣            
get return _messageModel; }
bubuko.com,布布扣            
set { _messageModel = value; }
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 执行抽象访问者的Visit()方法(从而执行抽象元素的方法)
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="abstractVisitor">抽象访问者</param>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public override string Accept(AbstractVisitor abstractVisitor)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
return abstractVisitor.Visit(this);
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 获取Message
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public abstract List<MessageModel> Get();
bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 插入Message
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public abstract bool Insert();
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

SqlMessage
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// Sql方式操作Message(ConcreteElement)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class SqlMessage : Message
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="mm">Message实体对象</param>

bubuko.com,布布扣        public SqlMessage(MessageModel mm)
bubuko.com,布布扣            : 
base(mm)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 获取Message
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public override List<MessageModel> Get()
bubuko.com,布布扣        
{
bubuko.com,布布扣            List
<MessageModel> l = new List<MessageModel>();
bubuko.com,布布扣            l.Add(
new MessageModel("SQL方式获取Message", DateTime.Now));
bubuko.com,布布扣
bubuko.com,布布扣            
return l;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 插入Message
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public override bool Insert()
bubuko.com,布布扣        
{
bubuko.com,布布扣            
// 代码略
bubuko.com,布布扣
            return true;
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

XmlMessage
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// Xml方式操作Message(ConcreteElement)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class XmlMessage : Message
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 构造函数
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="mm">Message实体对象</param>

bubuko.com,布布扣        public XmlMessage(MessageModel mm)
bubuko.com,布布扣            : 
base(mm)
bubuko.com,布布扣        
{
bubuko.com,布布扣 
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 获取Message
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public override List<MessageModel> Get()
bubuko.com,布布扣        
{
bubuko.com,布布扣            List
<MessageModel> l = new List<MessageModel>();
bubuko.com,布布扣            l.Add(
new MessageModel("XML方式获取Message", DateTime.Now));
bubuko.com,布布扣
bubuko.com,布布扣            
return l;
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 插入Message
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public override bool Insert()
bubuko.com,布布扣        
{
bubuko.com,布布扣            
// 代码略
bubuko.com,布布扣
            return true;
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

AbstractVisitor
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// 抽象访问者(Visitor)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public abstract class AbstractVisitor
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 执行抽象元素的方法
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="abstractElement">抽象元素</param>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public abstract string Visit(AbstractElement abstractElement);
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

InsertVisitor
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// Insert访问者(ConcreteVisitor)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class InsertVisitor : AbstractVisitor
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 执行Message的Insert()方法
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="abstractElement">抽象元素</param>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public override string Visit(AbstractElement abstractElement)
bubuko.com,布布扣        
{
bubuko.com,布布扣            Message m 
= abstractElement as Message;
bubuko.com,布布扣
bubuko.com,布布扣            
return m.Insert().ToString() + "<br />";
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

GetVisitor
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// Get访问者(ConcreteVisitor)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class GetVisitor : AbstractVisitor
bubuko.com,布布扣    
{
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 执行Message的Get()方法
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="abstractElement">抽象元素</param>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public override string Visit(AbstractElement abstractElement)
bubuko.com,布布扣        
{
bubuko.com,布布扣            Message m 
= abstractElement as Message;
bubuko.com,布布扣
bubuko.com,布布扣            
return m.Get()[0].Message + " " + m.Get()[0].PublishTime.ToString() + "<br />";
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

Messages
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Collections.Generic;
bubuko.com,布布扣
using System.Text;
bubuko.com,布布扣
bubuko.com,布布扣
namespace Pattern.Visitor
bubuko.com,布布扣
{
bubuko.com,布布扣    
/// <summary>
bubuko.com,布布扣    
/// Message集合(ObjectStructure)
bubuko.com,布布扣    
/// </summary>

bubuko.com,布布扣    public class Messages
bubuko.com,布布扣    
{
bubuko.com,布布扣        
private List<Message> _list = new List<Message>();
bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 添加一个Message对象
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="message">Message对象</param>

bubuko.com,布布扣        public void Attach(Message message)
bubuko.com,布布扣        
{
bubuko.com,布布扣            _list.Add(message);
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 移除一个Message对象
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="message">Message对象</param>

bubuko.com,布布扣        public void Detach(Message message)
bubuko.com,布布扣        
{
bubuko.com,布布扣            _list.Remove(message);
bubuko.com,布布扣        }

bubuko.com,布布扣
bubuko.com,布布扣        
/// <summary>
bubuko.com,布布扣        
/// 执行集合内所有Message对象的Accept()方法(执行抽象访问者的Visit()方法(从而执行抽象元素的方法))
bubuko.com,布布扣        
/// </summary>
bubuko.com,布布扣        
/// <param name="abstractVisitor">抽象访问者</param>
bubuko.com,布布扣        
/// <returns></returns>

bubuko.com,布布扣        public string Accept(AbstractVisitor abstractVisitor)
bubuko.com,布布扣        
{
bubuko.com,布布扣            
string s = "";
bubuko.com,布布扣            
foreach (Message m in _list)
bubuko.com,布布扣            
{
bubuko.com,布布扣                s 
+= m.Accept(abstractVisitor);
bubuko.com,布布扣            }

bubuko.com,布布扣
bubuko.com,布布扣            
return s;
bubuko.com,布布扣        }

bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣


Test
bubuko.com,布布扣using System;
bubuko.com,布布扣
using System.Data;
bubuko.com,布布扣
using System.Configuration;
bubuko.com,布布扣
using System.Collections;
bubuko.com,布布扣
using System.Web;
bubuko.com,布布扣
using System.Web.Security;
bubuko.com,布布扣
using System.Web.UI;
bubuko.com,布布扣
using System.Web.UI.WebControls;
bubuko.com,布布扣
using System.Web.UI.WebControls.WebParts;
bubuko.com,布布扣
using System.Web.UI.HtmlControls;
bubuko.com,布布扣
bubuko.com,布布扣
using Pattern.Visitor;
bubuko.com,布布扣
bubuko.com,布布扣
public partial class Visitor : System.Web.UI.Page
bubuko.com,布布扣
{
bubuko.com,布布扣    
protected void Page_Load(object sender, EventArgs e)
bubuko.com,布布扣    
{
bubuko.com,布布扣        Pattern.Visitor.Messages m 
= new Pattern.Visitor.Messages();
bubuko.com,布布扣
bubuko.com,布布扣        m.Attach(
new SqlMessage(new MessageModel("插入", DateTime.Now)));
bubuko.com,布布扣        m.Attach(
new XmlMessage(new MessageModel("插入", DateTime.Now)));
bubuko.com,布布扣
bubuko.com,布布扣        Response.Write(m.Accept(
new InsertVisitor()));
bubuko.com,布布扣        Response.Write(m.Accept(
new GetVisitor()));
bubuko.com,布布扣    }

bubuko.com,布布扣}

bubuko.com,布布扣

运行结果
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


OK
[源码下载]

乐在其中设计模式(C#) - 访问者模式(Visitor Pattern),布布扣,bubuko.com

乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)

原文:http://www.cnblogs.com/lonelyxmas/p/3590366.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!