分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

Definition

Represent an operation to be performed on the elements of an object structure. Visitor lets you define a new operation without changing the classes of the elements on which it operates.

Participants

The classes and/or objects participating in this pattern are:

  • Visitor (Visitor)

    • Declares a Visit operation for each class of ConcreteElement in the object structure. The operation's name and signature identifies the class that sends the Visit request to the visitor. That lets the visitor determine the concrete class of the element being visited. Then the visitor can access the elements directly through its particular interface.
  • ConcreteVisitor (IncomeVisitor, VacationVisitor) 
    • Implements each operation declared by Visitor. Each operation implements a fragment of the algorithm defined for the corresponding class or object in the structure. ConcreteVisitor provides the context for the algorithm and stores its local state. This state often accumulates results during the traversal of the structure.
  • Element (Element) 
    • Defines an Accept operation that takes a visitor as an argument.
  • ConcreteElement (Employee) 
    • Implements an Accept operation that takes a visitor as an argument.
  • ObjectStructure (Employees) 
    • Can enumerate its elements
    • May provide a high-level interface to allow the visitor to visit its elements
    • May either be a Composite (pattern) or a collection such as a list or a set

Sample Code in C#


This structural code demonstrates the Visitor pattern in which an object traverses an object structure and performs the same operation on each node in this structure. Different visitor objects define different operations.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">//   Respect the work.// </copyright>// <summary>//   Visitor pattern - Structural example// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections.Generic;    /// <summary>    /// Startup class for Structural Visitor Design Pattern.    /// </summary>    public static class Program    {        /// <summary>        /// The main.        /// </summary>        public static void Main()        {            // Setup structure            ObjectStructure o = new ObjectStructure();            o.Attach(new ConcreteElementA());            o.Attach(new ConcreteElementB());            // Create visitor objects            ConcreteVisitor1 v1 = new ConcreteVisitor1();            ConcreteVisitor2 v2 = new ConcreteVisitor2();            // Structure accepting visitors            o.Accept(v1);            o.Accept(v2);        }    }    /// <summary>    /// The 'Visitor' abstract class    /// </summary>    public abstract class Visitor    {        /// <summary>        /// Visit concrete element a.        /// </summary>        /// <param name="concreteElementA">        /// The concrete element a.        /// </param>        public abstract void VisitConcreteElementA(ConcreteElementA concreteElementA);        /// <summary>        /// Visit concrete element b.        /// </summary>        /// <param name="concreteElementB">        /// The concrete element b.        /// </param>        public abstract void VisitConcreteElementB(ConcreteElementB concreteElementB);    }    /// <summary>    /// A 'ConcreteVisitor' class    /// </summary>    public class ConcreteVisitor1 : Visitor    {        /// <summary>        /// Visit concrete element a.        /// </summary>        /// <param name="concreteElementA">        /// The concrete element a.        /// </param>        public override void VisitConcreteElementA(ConcreteElementA concreteElementA)        {            Console.WriteLine("{0} visited by {1}", concreteElementA.GetType().Name, this.GetType().Name);        }        /// <summary>        /// Visit concrete element b.        /// </summary>        /// <param name="concreteElementB">        /// The concrete element b.        /// </param>        public override void VisitConcreteElementB(ConcreteElementB concreteElementB)        {            Console.WriteLine("{0} visited by {1}", concreteElementB.GetType().Name, this.GetType().Name);        }    }    /// <summary>    /// A 'ConcreteVisitor' class    /// </summary>    public class ConcreteVisitor2 : Visitor    {        /// <summary>        /// Visit concrete element a.        /// </summary>        /// <param name="concreteElementA">        /// The concrete element a.        /// </param>        public override void VisitConcreteElementA(ConcreteElementA concreteElementA)        {            Console.WriteLine("{0} visited by {1}", concreteElementA.GetType().Name, this.GetType().Name);        }        /// <summary>        /// Visit concrete element b.        /// </summary>        /// <param name="concreteElementB">        /// The concrete element b.        /// </param>        public override void VisitConcreteElementB(ConcreteElementB concreteElementB)        {            Console.WriteLine("{0} visited by {1}", concreteElementB.GetType().Name, this.GetType().Name);        }    }    /// <summary>    /// The 'Element' abstract class    /// </summary>    public abstract class Element    {        /// <summary>        /// The accept.        /// </summary>        /// <param name="visitor">        /// The visitor.        /// </param>        public abstract void Accept(Visitor visitor);    }    /// <summary>    /// A 'ConcreteElement' class    /// </summary>    public class ConcreteElementA : Element    {        /// <summary>        /// The accept.        /// </summary>        /// <param name="visitor">        /// The visitor.        /// </param>        public override void Accept(Visitor visitor)        {            visitor.VisitConcreteElementA(this);        }    }    /// <summary>    /// A 'ConcreteElement' class    /// </summary>    public class ConcreteElementB : Element    {        /// <summary>        /// The accept.        /// </summary>        /// <param name="visitor">        /// The visitor.        /// </param>        public override void Accept(Visitor visitor)        {            visitor.VisitConcreteElementB(this);        }    }    /// <summary>    /// The 'ObjectStructure' class    /// </summary>    public class ObjectStructure    {        /// <summary>        /// The elements.        /// </summary>        private readonly List<Element> elements = new List<Element>();        /// <summary>        /// The attach.        /// </summary>        /// <param name="element">        /// The element.        /// </param>        public void Attach(Element element)        {            this.elements.Add(element);        }        /// <summary>        /// The accept.        /// </summary>        /// <param name="visitor">        /// The visitor.        /// </param>        public void Accept(Visitor visitor)        {            foreach (Element element in this.elements)            {                element.Accept(visitor);            }        }    }}// Output:/*ConcreteElementA visited by ConcreteVisitor1ConcreteElementB visited by ConcreteVisitor1ConcreteElementA visited by ConcreteVisitor2ConcreteElementB visited by ConcreteVisitor2*/

This real-world code demonstrates the Visitor pattern in which two objects traverse a list of Employees and performs the same operation on each Employee.  The two visitor objects define different operations -- one adjusts vacation days and the other income.

// --------------------------------------------------------------------------------------------------------------------// <copyright company="Chimomo's Company" file="Program.cs">// Respect the work.// </copyright>// <summary>// Real-World Visitor Design Pattern.// </summary>// --------------------------------------------------------------------------------------------------------------------namespace CSharpLearning{    using System;    using System.Collections.Generic;    /// <summary>    /// The 'Visitor' interface    /// </summary>    public interface IVisitor    {        #region Public Methods and Operators        /// <summary>        /// The visit.        /// </summary>        /// <param name="element">        /// The element.        /// </param>        void Visit(Element element);        #endregion    }    /// <summary>    /// The 'Element' abstract class    /// </summary>    public abstract class Element    {        #region Public Methods and Operators        /// <summary>        /// The accept.        /// </summary>        /// <param name="visitor">        /// The visitor.        /// </param>        public abstract void Accept(IVisitor visitor);        #endregion    }    /// <summary>    /// Startup class for Real-World Visitor Design Pattern.    /// </summary>    internal static class Program    {        #region Methods        /// <summary>        /// Entry point into console application.        /// </summary>        private static void Main()        {            // Setup employee collection            var e = new Employees();            e.Attach(new Clerk());            e.Attach(new Director());            e.Attach(new President());            // Employees are 'visited'            e.Accept(new IncomeVisitor());            e.Accept(new VacationVisitor());        }        #endregion    }    /// <summary>    /// A 'ConcreteVisitor' class    /// </summary>    internal class IncomeVisitor : IVisitor    {        #region Public Methods and Operators        /// <summary>        /// The visit.        /// </summary>        /// <param name="element">        /// The element.        /// </param>        public void Visit(Element element)        {            var employee = element as Employee;            // Provide 10% pay raise            employee.Income *= 1.10;            Console.WriteLine("{0} {1}'s new income: {2:C}", employee.GetType().Name, employee.Name, employee.Income);        }        #endregion    }    /// <summary>    /// A 'ConcreteVisitor' class    /// </summary>    internal class VacationVisitor : IVisitor    {        #region Public Methods and Operators        /// <summary>        /// The visit.        /// </summary>        /// <param name="element">        /// The element.        /// </param>        public void Visit(Element element)        {            var employee = element as Employee;            // Provide 3 extra vacation days            employee.VacationDays += 3;            Console.WriteLine("{0} {1}'s new vacation days: {2}", employee.GetType().Name, employee.Name, employee.VacationDays);        }        #endregion    }    /// <summary>    /// The 'ConcreteElement' class    /// </summary>    internal class Employee : Element    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Employee"/> class.        /// </summary>        /// <param name="name">        /// The name.        /// </param>        /// <param name="income">        /// The income.        /// </param>        /// <param name="vacationDays">        /// The vacation days.        /// </param>        public Employee(string name, double income, int vacationDays)        {            this.Name = name;            this.Income = income;            this.VacationDays = vacationDays;        }        #endregion        // Gets or sets income        #region Public Properties        /// <summary>        /// Gets or sets the income.        /// </summary>        public double Income { get; set; }        /// <summary>        /// Gets or sets the name.        /// </summary>        public string Name { get; set; }        /// <summary>        /// Gets or sets the vacation days.        /// </summary>        public int VacationDays { get; set; }        #endregion        #region Public Methods and Operators        /// <summary>        /// The accept.        /// </summary>        /// <param name="visitor">        /// The visitor.        /// </param>        public override void Accept(IVisitor visitor)        {            visitor.Visit(this);        }        #endregion    }    /// <summary>    /// The 'ObjectStructure' class    /// </summary>    internal class Employees    {        #region Fields        /// <summary>        /// The _employees.        /// </summary>        private List<Employee> employees = new List<Employee>();        #endregion        #region Public Methods and Operators        /// <summary>        /// The accept.        /// </summary>        /// <param name="visitor">        /// The visitor.        /// </param>        public void Accept(IVisitor visitor)        {            foreach (Employee e in this.employees)            {                e.Accept(visitor);            }            Console.WriteLine();        }        /// <summary>        /// The attach.        /// </summary>        /// <param name="employee">        /// The employee.        /// </param>        public void Attach(Employee employee)        {            this.employees.Add(employee);        }        /// <summary>        /// The detach.        /// </summary>        /// <param name="employee">        /// The employee.        /// </param>        public void Detach(Employee employee)        {            this.employees.Remove(employee);        }        #endregion    }    // Three employee types    /// <summary>    /// The clerk.    /// </summary>    internal class Clerk : Employee    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Clerk"/> class.        /// </summary>        public Clerk()            : base("Hank", 25000.0, 14)        {        }        #endregion    }    /// <summary>    /// The director.    /// </summary>    internal class Director : Employee    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="Director"/> class.        /// </summary>        public Director()            : base("Elly", 35000.0, 16)        {        }        #endregion    }    /// <summary>    /// The president.    /// </summary>    internal class President : Employee    {        // Constructor        #region Constructors and Destructors        /// <summary>        /// Initializes a new instance of the <see cref="President"/> class.        /// </summary>        public President()            : base("Dick", 45000.0, 21)        {        }        #endregion    }}// Output:/*Clerk Hank's new income: $27,500.00Director Elly's new income: $38,500.00President Dick's new income: $49,500.00Clerk Hank's new vacation days: 17Director Elly's new vacation days: 19President Dick's new vacation days: 24*/

给我老师的人工智能教程打call!http://blog.csdn.net/jiangjunshow

Design Pattern - Visitor(C#)相关推荐

  1. Design Pattern - State(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  2. Design Pattern - Observer(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  3. Design Pattern - Mediator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  4. Design Pattern - Memento(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  5. Design Pattern - Iterator(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  6. Design Pattern - Interpreter(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  7. Design Pattern - Command (C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  8. Design Pattern - Flyweight(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

  9. Design Pattern - Proxy(C#)

    分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! Defi ...

最新文章

  1. 管理员信息管理之获取管理员用户列表数据
  2. session和Cookie的区别
  3. pybind 编码h264
  4. linux系统下卷组管理,Linux LVM卷组管理
  5. 表达式ya是不合法的c语言标识符,《C语言程序设计》试题3及答案
  6. day15 webUI自动化
  7. cookie和session 以及Django中应用
  8. CCF CSP201912-1 报数
  9. 云原生是趋势吗?学习 K8s 和 Docker 的意义在哪里?
  10. Cadence OrCAD Capture交叉参考报表生成方法图文教程
  11. Python之pygame安装教程
  12. android立方体旋转动画,如何画出一个旋转的立方体
  13. 拉杰尔安卓服务器注册上限,航海王启航409区圣地玛丽杰尔开服时间表_航海王启航新区开服预告_第一手游网手游开服表...
  14. 【从零开始学习 UVM】6.4、UVM 激励产生 —— uvm_do 宏详解
  15. 多人扑克游戏:99分游戏规则介绍
  16. 前端vue从后台取到数据(数组对象)后遍历给对象添加属性,修改这个属性为什么没有用?
  17. 振兴当当,李国庆如何逐鹿中原?
  18. DataDig 5.0.0::自动提取论文图表中曲线数据的软件
  19. 数据库-高级SQL语句
  20. 如何将APE及FLAC格式文件刻录成CD

热门文章

  1. A 元素[HTML 4.01]
  2. Go 语言 Session机制和 Cookie机制
  3. Java开发中业务层入参校验详细解析
  4. ArrayList 源码阅读记录
  5. 帕金森患者的新曙光!AI无线智能探测系统诞生
  6. 组件(2):使用Prop下发数据
  7. 使用common-fileUpload制作文件上传【转】
  8. 关于上个Flex-Jsp-DB例子中Flex和Jsp传递中文参数问题的解决方法!(Tomcat服务器)...
  9. linux c 时间函数 time difftime 简介
  10. golang 实现 while 和 do……while 循环