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

[索引页][源码下载]

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

作者:webabcd





介绍

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





示例

有一个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;



            ].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





参考

http://www.dofactory.com/Patterns/PatternVisitor.aspx





OK

[源码下载]

乐在其中设计模式(C#) - 访问者模式(Visitor Pattern)的更多相关文章

  1. 二十四种设计模式:访问者模式(Visitor Pattern)

    访问者模式(Visitor Pattern) 介绍表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作. 示例有一个Message实体类,某些对象对 ...

  2. [设计模式] 23 访问者模式 visitor Pattern

    在GOF的<设计模式:可复用面向对象软件的基础>一书中对访问者模式是这样说的:表示一个作用于某对象结构中的各元素的操作.它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作.访问 ...

  3. 访问者模式(Visitor Pattern)——操作复杂对象结构

    模式概述 在软件开发中,可能会遇到操作复杂对象结构的场景,在该对象结构中存储了多个不同类型的对象信息,而且对同一对象结构中的元素的操作方式并不唯一,可能需要提供多种不同的处理方式,还有可能增加新的处理 ...

  4. 乐在其中设计模式(C#) - 提供者模式(Provider Pattern)

    原文:乐在其中设计模式(C#) - 提供者模式(Provider Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 提供者模式(Provider Pattern) 作者:weba ...

  5. 乐在其中设计模式(C#) - 策略模式(Strategy Pattern)

    原文:乐在其中设计模式(C#) - 策略模式(Strategy Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 策略模式(Strategy Pattern) 作者:webabc ...

  6. 乐在其中设计模式(C#) - 状态模式(State Pattern)

    原文:乐在其中设计模式(C#) - 状态模式(State Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 状态模式(State Pattern) 作者:webabcd 介绍 允 ...

  7. 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern)

    原文:乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 备忘录模式(Memento Pattern) 作者:webabc ...

  8. 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern)

    原文:乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 迭代器模式(Iterator Pattern) 作者:weba ...

  9. 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern)

    原文:乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) [索引页][源码下载] 乐在其中设计模式(C#) - 解释器模式(Interpreter Pattern) 作 ...

随机推荐

  1. 使用Runtime.getRuntime().exec()方法的几个陷阱 (转)

    Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...

  2. Zigbee开发(1)

    只是研究zigbee的技术,也许后续的博客不会有很及时的更新,有时间 写一点东西能让大家有所收获吧. 环境搭建 Windows 64位的操作系统 IAR7.6 for 8051 ZStack CC25 ...

  3. Android入门之login设计

    效果图: MainActivity.java package jk.quickpay.login; import jk.quickpay.login.FileService; import java. ...

  4. UVA 620 Cellular Structure (dp)

     Cellular Structure  A chain of connected cells of two types A and B composes a cellular structure o ...

  5. WebService的相关使用

    近期公司项目使用WebService ,这里简单做个总结. 事实上详细使用细节有些情况下须要改,还须要看实际情况,须要与server联调,详细沟通. 比方公司连接,非要把envelope.dotNet ...

  6. mysql视图学习总结

    转自http://www.cnblogs.com/wangtao_20/archive/2011/02/24/1964276.html 一.使用视图的理由是什么? 1.安全性.一般是这样做的:创建一个 ...

  7. SE 2014年4月8日

    1.路由引入的作用? 当网络中运行多种路由协议的时候,由于不同协议的路由算法和度量值等均不相同,路由引入可以将不同协议的路由引入到当前的路由协议中,保证网络的互通. 对比单向入和双向入 单向引入是只将 ...

  8. 物联网 开发板 基于ESP8266

    The ESP8266 The ESP8266 is a highly integrated chip designed for the needs of an increasingly connec ...

  9. [置顶] ※数据结构※→☆线性表结构(list)☆============双向链表结构(list double)(三)

    双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱.所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点. ~~~~~~~~~~~~ ...

  10. hadoop在实现kmeans算法——一个mapreduce实施

    写mapreduce程序实现kmeans算法.我们的想法可能是 1. 次迭代后的质心 2. map里.计算每一个质心与样本之间的距离,得到与样本距离最短的质心,以这个质心作为key,样本作为value ...