CSharp: Visitor Pattern

 

    /// <summary>
    /// Summary description for Employee.
    /// Visitor Pattern 访问者模式
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public class Employee
    {
        int sickDays, vacDays;
        float salary;
        string name;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="salary"></param>
        /// <param name="vDays"></param>
        /// <param name="sDays"></param>
        public Employee(string name, float salary, int vDays, int sDays)
        {
            this.name = name;
            this.salary = salary;
            sickDays = sDays;
            vacDays = vDays;
        }
        /// <summary>
        /// 
        /// </summary>
        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        /// <summary>
        /// 
        /// </summary>
        public float Salary
        {

            get { return salary; }
            set { salary = value; }

        }
        /// <summary>
        /// 
        /// </summary>
        public int SickDays
        {

            get { return sickDays; }
            set { sickDays = value; }

        }
        /// <summary>
        /// 
        /// </summary>
        public int VacDays
        {

            get { return vacDays; }
            set { vacDays = value; }

        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string getName()
        {
            return name;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int getSickDays()
        {
            return sickDays;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int getVacDays()
        {
            return vacDays;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public float getSalary()
        {
            return salary;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="v"></param>
        public virtual void accept(Visitor v)
        {
            v.visit(this);
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public string toString()
        {
            return name + "," + salary.ToString() + "," + sickDays.ToString() + "," + vacDays.ToString() + "";
        }
    }

  

    /// <summary>
    /// Summary description for Visitor.
    /// Visitor Pattern 访问者模式
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public abstract class Visitor
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="emp"></param>
        public abstract void visit(Employee emp);

        /// <summary>
        /// 
        /// </summary>
        /// <param name="bos"></param>
        public abstract void visit(Boss bos);
    }

  

   /// <summary>
    /// Summary description for Manager.\
    /// Visitor Pattern 访问者模式
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public class Manager : Employee
    {
        private int bonusDays;
        /// <summary>
        /// 
        /// </summary>
        /// <param name="nm"></param>
        /// <param name="slry"></param>
        /// <param name="vDays"></param>
        /// <param name="sDays"></param>
        public Manager(string nm, float slry, int vDays, int sDays) :
            base(nm, slry, vDays, sDays)
        {
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bd"></param>
        public void setBonusDays(int bd)
        {
            bonusDays = bd;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int getBonusDays()
        {
            return bonusDays;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="v"></param>
        public override void accept(Visitor v)
        {
            v.visit(this);
        }
    }

  

  /// <summary>
    /// Summary description for Boss.
    /// Visitor Pattern 访问者模式
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public class Boss : Employee
    {
        private int bonusDays;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="name"></param>
        /// <param name="salary"></param>
        /// <param name="vdays"></param>
        /// <param name="sdays"></param>
        public Boss(string name, float salary, int vdays, int sdays)
            : base(name, salary, vdays, sdays)
        { }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bdays"></param>
        public void setBonusDays(int bdays)
        {
            bonusDays = bdays;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int getBonusDays()
        {
            return bonusDays;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="v"></param>
        public override void accept(Visitor v)
        {
            v.visit(this);
        }
    }

  

  /// <summary>
    /// Summary description for VacationVisitor.
    /// Visitor Pattern 访问者模式
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public class VacationVisitor : Visitor
    {
        private int totalDays;
        /// <summary>
        /// 
        /// </summary>
        public VacationVisitor()
        {
            totalDays = 0;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public int getTotalDays()
        {
            return totalDays;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="emp"></param>
        public override void visit(Employee emp)
        {
            totalDays += emp.getVacDays();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="bos"></param>
        public override void visit(Boss bos)
        {
            totalDays += bos.getVacDays();
        }
    }

  

调用:

   /// <summary>
    /// Visitor Pattern 访问者模式
    ///20220918
    /// geovindu,Geovin Du,涂聚文
    /// </summary>
    public partial class VisitorPatternForm : Form
    {

        private List<Employee>  empls;
        private Employee empl;
        private Boss bos;
        /// <summary>
        /// 
        /// </summary>
        private void init()
        {
            int i = 0;
            empls = new List<Employee>();
            empl = new Employee("Susan Bear", 55000, 12, 1);
            empls.Add(empl);
            empl = new Employee("Adam Gehr", 150000, 9, 0);
            empls.Add(empl);
            empl = new Employee("Fred Harris", 50000, 15, 2);
            empls.Add(empl);
            empl = new Employee("David Oakley", 57000, 12, 2);
            empls.Add(empl);
            empl = new Employee("Larry Thomas", 100000, 20, 6);
            empls.Add(empl);
            bos = new Boss("Leslie Susi", 175000, 16, 4);
            empls.Add(empl);
            bos.setBonusDays(12);
            empls.Add(bos);
            bos = new Boss("Laurence Byerly", 35000, 17, 6);
            bos.setBonusDays(17);
            empls.Add(bos);
            
         
        }
        /// <summary>
        /// 
        /// </summary>
        public VisitorPatternForm()
        {
            InitializeComponent();
            init();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void VisitorPatternForm_Load(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource = empls;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btCompute_Click(object sender, EventArgs e)
        {
            VacationVisitor vac = new VacationVisitor();
            VacationVisitor bvac = new VacationVisitor();
            for (int i = 0; i < empls.Count; i++)
            {
                empls[i].accept(vac);      //get the employee
                empls[i].accept(bvac);
            }
            lsVac.Items.Add("Total vacation days=" + vac.getTotalDays().ToString());
            lsVac.Items.Add("Total boss vacation days=" + bvac.getTotalDays().ToString());

            this.dataGridView1.DataSource = empls;
        }
    }

  

输出:

 

posted @ 2022-10-02 19:25  ®Geovin Du Dream Park™  阅读(19)  评论(0编辑  收藏  举报